使用Xib创建UIView,使用纯代码AotoLayout等

2016-06-30  本文已影响97人  952625a28d0d
#import "TemperatureView.h"

@interface TemperatureView ()
@property (strong, nonatomic) IBOutlet UIView *view;
@property (weak, nonatomic) IBOutlet UILabel *temperatureLabel;
@property (weak, nonatomic) IBOutlet UISegmentedControl *unitSegmented;

- (IBAction)changeUnit:(UISegmentedControl *)sender;
@end

@implementation TemperatureView

-(instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self setUp];
    }
    return self;
}

-(instancetype)initWithCoder:(NSCoder *)aDecoder {
    self = [super initWithCoder:aDecoder];
    if (self) {
        [self setUp];
    }
    return self;
}

#pragma mark - 赋值则会走set方法
-(void)setCelsius:(float)celsius {
    _celsius = celsius;
    _fahrenheit = 32 + celsius * 1.8;
    [self updateLabel];
}

#pragma mark - 初始化赋值并刷新Label
-(void)setFahrenheit:(float)fahrenheit {
    _fahrenheit = fahrenheit;
    _celsius = (fahrenheit - 32) / 1.8;
    [self updateLabel];
}

#pragma mark - 按钮点击刷新Label
-(void)updateLabel {
    if (self.unitSegmented.selectedSegmentIndex == 0) {
        NSString * text = [NSString stringWithFormat:@"%.1f℃", self.celsius];
        self.temperatureLabel.text = text;
    } else {
        NSString * text = [NSString stringWithFormat:@"%.1f℉", self.fahrenheit];
        self.temperatureLabel.text = text;
    }
}

-(void)setUp {
    
    // load boundle
#if TARGET_INTERFACE_BUILDER
    NSBundle *bundle = [NSBundle bundleForClass:[self class]];
    [bundle loadNibNamed:@"TemperatureView" owner:self options:nil];
#else
    [[NSBundle mainBundle]loadNibNamed:@"TemperatureView" owner:self options:nil];
#endif
    

    self.view.translatesAutoresizingMaskIntoConstraints = NO;
    
    // 初始化约束
    NSLayoutConstraint * l = [NSLayoutConstraint constraintWithItem:self.view
                                                          attribute:NSLayoutAttributeLeading
                                                          relatedBy:NSLayoutRelationEqual
                                                             toItem:self
                                                          attribute:NSLayoutAttributeLeading
                                                         multiplier:1.0
                                                           constant:0];
    NSLayoutConstraint * r = [NSLayoutConstraint constraintWithItem:self.view
                                                          attribute:NSLayoutAttributeTrailing
                                                          relatedBy:NSLayoutRelationEqual
                                                             toItem:self
                                                          attribute:NSLayoutAttributeTrailing
                                                         multiplier:1.0
                                                           constant:0];
    NSLayoutConstraint * t = [NSLayoutConstraint constraintWithItem:self.view
                                                          attribute:NSLayoutAttributeTop
                                                          relatedBy:NSLayoutRelationEqual
                                                             toItem:self
                                                          attribute:NSLayoutAttributeTop
                                                         multiplier:1.0
                                                           constant:0];
    
    NSLayoutConstraint * b = [NSLayoutConstraint constraintWithItem:self.view
                                                          attribute:NSLayoutAttributeBottom
                                                          relatedBy:NSLayoutRelationEqual
                                                             toItem:self
                                                          attribute:NSLayoutAttributeBottom
                                                         multiplier:1.0
                                                           constant:0];
    
    // 添加View
    [self addSubview:self.view];
    
    // 添加约束
    [NSLayoutConstraint activateConstraints:@[l, r, t, b]];
    
    // 设置文字大小
    self.temperatureLabel.font = [UIFont systemFontOfSize:MIN(self.bounds.size.width, self.bounds.size.height) / 5];
}

- (IBAction)changeUnit:(UISegmentedControl *)sender {
    [self updateLabel];
}
@end```

![xib创建UIView.gif](https://img.haomeiwen.com/i189984/7006ed93220ff5aa.gif?imageMogr2/auto-orient/strip)
上一篇 下一篇

猜你喜欢

热点阅读