在启用Autolayout时未设置自定义UIView子类背景颜色

时间:2014-09-23 04:59:31

标签: ios objective-c uiview autolayout

我正在使用 AutoLayout 创建UIView的子类,并且无法让它的背景颜色在我的生活中改变。有人可以看看简单的代码,并验证我正在做UIViewPCCGenericRatingView)的子类中需要完成的所有事情。我在SO上读到,设置背景颜色只有在设置了框架时才有效。在评估约束后,不应该将我的框架设置为CGRectZero以外的其他东西吗?我正在viewDidAppear:生命周期方法中打印出视图的框架,但它似乎是CGRectZero。

编辑: 将帧传递给视图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

1 个答案:

答案 0 :(得分:0)

打印出view属性以跟踪它是否指向您正在引用的视图或其他[view setBackgroundColor:[UIColor yellowColor]]