2014-09-23 93 views
0

我正在創建一個子類UIViewAutoLayout無法獲取它的背景顏色來改變我的生活。有人可以看看簡單的代碼,並驗證我正在做的所有事情都需要在UIViewPCCGenericRatingView)的子類中完成。我讀過SO,設置背景色只在幀被設置時才起作用。在評估約束之後,它不應該把我的框架設置爲CGRectZero以外的其他東西嗎?我正在打印viewDidAppear:生命週期方法中的視圖框架,但它看起來像是CGRectZero。自定義UIView子類背景顏色未啓用Autolayout設置

編輯: 當幀被傳遞到視圖initWithFrame初始值設定項時,會顯示背景顏色。否則,現在顯示。

PCCGenericRatingView.h文件:

#import <UIKit/UIKit.h> 

@interface PCCGenericRatingView : UIView 

@property (nonatomic, strong) UILabel *titleLabel; 
@property (nonatomic, strong) UILabel *messageLabel; 

- (instancetype)initWithTitle:(NSString *)title andMessage:(NSString *)message; 
+ (instancetype)genericRatingViewWithTitle:(NSString *)title andMessage:(NSString *)message; 

@end 

PCCGenericRatingView.m文件:

#import "PCCGenericRatingView.h" 

@implementation PCCGenericRatingView 

- (instancetype)initWithTitle:(NSString *)title andMessage:(NSString *)message { 
    if (self = [super init]) { 
     self.titleLabel = [[UILabel alloc] init]; 
     self.messageLabel = [[UILabel alloc] init]; 

     _titleLabel.text = title; 
     _messageLabel.text = message; 

     [self customizeView]; 
    } 

    return self; 
} 

- (void)customizeView { 
    [self addSubview:_titleLabel]; 
    [self addSubview:_messageLabel]; 

    [_titleLabel setTranslatesAutoresizingMaskIntoConstraints:NO]; 
    [_messageLabel setTranslatesAutoresizingMaskIntoConstraints:NO]; 

    [_titleLabel setBackgroundColor:[UIColor yellowColor]]; 
    [_messageLabel setBackgroundColor:[UIColor yellowColor]]; 

    [_messageLabel setNumberOfLines:0]; 

    [_messageLabel setTextAlignment:NSTextAlignmentCenter]; 
    [_titleLabel setTextAlignment:NSTextAlignmentCenter]; 

    [_titleLabel setFont:[UIFont fontWithName:@"HelveticaNeue-Bold" size:24.0f]]; 
    [_messageLabel setFont:[UIFont fontWithName:@"HelveticaNeue-Thin" size:18.0f]]; 
} 

+ (instancetype)genericRatingViewWithTitle:(NSString *)title andMessage:(NSString *)message { 
    return [[PCCGenericRatingView alloc] initWithTitle:title andMessage:message]; 
} 

+ (BOOL)requiresConstraintBasedLayout { 
    return YES; 
} 

- (BOOL)translatesAutoresizingMaskIntoConstraints { 
    return NO; 
} 

-(void)updateConstraints { 

    CGRect windowRect = [UIScreen mainScreen].bounds; 
    NSDictionary *metrics = @{ 
    @"height" : @(windowRect.size.height), 
    @"width" : @(windowRect.size.width) 
    }; 

    NSDictionary *viewsDictionary = NSDictionaryOfVariableBindings(self); 

    [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:[self(==width)]" options:0 metrics:metrics views:viewsDictionary]]; 
    [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[self(==height)]" options:0 metrics:metrics views:viewsDictionary]]; 


    [self addConstraint:[NSLayoutConstraint constraintWithItem:_titleLabel 
                attribute:NSLayoutAttributeTop 
                relatedBy:NSLayoutRelationEqual 
                 toItem:self 
                attribute:NSLayoutAttributeTop 
                multiplier:1.0f 
                 constant:35.0f]]; 

    [self addConstraint:[NSLayoutConstraint constraintWithItem:_titleLabel 
                attribute:NSLayoutAttributeCenterX 
                relatedBy:NSLayoutRelationEqual 
                 toItem:self 
                attribute:NSLayoutAttributeCenterX 
                multiplier:1.0f 
                 constant:0.0f]]; 

    [self addConstraint:[NSLayoutConstraint constraintWithItem:_titleLabel 
                attribute:NSLayoutAttributeWidth 
                relatedBy:NSLayoutRelationEqual 
                 toItem:self 
                attribute:NSLayoutAttributeWidth 
                multiplier:1.0f 
                 constant:0.0f]]; 

    [self addConstraint:[NSLayoutConstraint constraintWithItem:_titleLabel 
                attribute:NSLayoutAttributeHeight 
                relatedBy:NSLayoutRelationEqual 
                 toItem:nil 
                attribute:NSLayoutAttributeNotAnAttribute 
                multiplier:1.0f 
                 constant:30.0f]]; 

    [self addConstraint:[NSLayoutConstraint constraintWithItem:_messageLabel 
                attribute:NSLayoutAttributeBottom 
                relatedBy:NSLayoutRelationEqual 
                 toItem:self 
                attribute:NSLayoutAttributeBottom 
                multiplier:1.0f 
                 constant:-40.0f]]; 

    [self addConstraint:[NSLayoutConstraint constraintWithItem:_messageLabel 
                attribute:NSLayoutAttributeCenterX 
                relatedBy:NSLayoutRelationEqual 
                 toItem:self 
                attribute:NSLayoutAttributeCenterX 
                multiplier:1.0f 
                 constant:0.0f]]; 

    [self addConstraint:[NSLayoutConstraint constraintWithItem:_messageLabel 
                attribute:NSLayoutAttributeWidth 
                relatedBy:NSLayoutRelationEqual 
                 toItem:self 
                attribute:NSLayoutAttributeWidth 
                multiplier:1.0f 
                 constant:0.0f]]; 

    [self addConstraint:[NSLayoutConstraint constraintWithItem:_messageLabel 
                attribute:NSLayoutAttributeHeight 
                relatedBy:NSLayoutRelationEqual 
                 toItem:nil 
                attribute:NSLayoutAttributeNotAnAttribute 
                multiplier:1.0f 
                 constant:50.0f]]; 

    [super updateConstraints]; 
} 

@end 

父視圖控制器:

#import "PCCLeaveRatingViewController.h" 
#import "PCCGenericRating.h" 
#import "PCCSliderRatingView.h" 

@interface PCCLeaveRatingViewController() 

@end 

@implementation PCCLeaveRatingViewController 

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
{ 
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
    if (self) { 
     // Custom initialization 
    } 
    return self; 
} 


- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    self.dataSource = @[ [[PCCGenericRating alloc] initWithTitle:@"Easiness" 
                 andMessage:@"WHAT A JOKERRRR" 
                andVariatons:@[ @"very easy", @"easy", @"moderate", @"hard", @"very hard"]], 
         ]; 

    [self initView]; 

} 

- (void)viewDidAppear:(BOOL)animated { 
    [super viewDidAppear:animated]; 
} 

- (void)initView { 
    [self setupConstraints]; 
    [self addViewController]; 

} 

- (void)setupConstraints { 
    CGRect windowFrame = [[UIApplication sharedApplication].delegate window].frame; 
    CGFloat windowWidth = windowFrame.size.width * self.dataSource.count; 

    [self.containerView addConstraint:[NSLayoutConstraint constraintWithItem:self.containerView 
                    attribute:NSLayoutAttributeWidth 
                    relatedBy:NSLayoutRelationEqual 
                     toItem:nil 
                    attribute:NSLayoutAttributeNotAnAttribute 
                    multiplier:1.0f 
                    constant:windowWidth]]; 

    [self.containerView addConstraint:[NSLayoutConstraint constraintWithItem:self.containerView 
                    attribute:NSLayoutAttributeHeight 
                    relatedBy:NSLayoutRelationEqual 
                     toItem:nil 
                    attribute:NSLayoutAttributeNotAnAttribute 
                    multiplier:1.0f 
                    constant:windowFrame.size.height]]; 
} 

- (void)addViewController { 
    PCCGenericRating *rating = self.dataSource[0]; 
    PCCGenericRatingView *view = [PCCGenericRatingView genericRatingViewWithTitle:rating.title andMessage:rating.message]; 
    [view setBackgroundColor:[UIColor yellowColor]]; 

    const CGFloat viewWidth = [UIScreen mainScreen].bounds.size.width; 
    NSDictionary *viewsDictionary = NSDictionaryOfVariableBindings(view); 
    NSDictionary *metrics = @{ @"viewWidth" : @(viewWidth) }; 

    [_containerView addSubview:view]; 

    [view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:[view(==viewWidth)]" options:0 metrics:metrics views:viewsDictionary]]; 
    [self.containerView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[view]|" options:0 metrics:nil views:viewsDictionary]]; 


} 

@end 

回答

0

打印出來的視圖屬性來跟蹤它是否指向你正在查看的視圖或其他[查看setBackgroundColor:[UIColor yellowColor]]