2014-09-23 91 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]]