当UIScrollView遇上AutoLayout

2016-05-03  本文已影响164人  流星大石头
@interface ViewController ()

@property (nonatomic,strong) UIScrollView* scrollView;

@property (nonatomic,strong) UIView* containerView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view addSubview:self.scrollView];
    [self.scrollView addSubview:self.containerView];
    
    [self layoutPageSubviews];

}

#pragma mark - 添加约束
- (void) layoutPageSubviews {
    
    //   使用Masonry第三方框架
     [self.scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
          make.edges.equalTo(self.view);
     }];
   
    [self.containerView mas_makeConstraints:^(MASConstraintMaker *make) {
   
         make.edges.equalTo(self.scrollView);
         make.width.equalTo(self.scrollView);
      }];
  
#pragma mark - 懒加载
- (UIScrollView *)scrollView{

    if (_scrollView==nil) {
        
        _scrollView = [[UIScrollView alloc] init];
        _scrollView.backgroundColor = [UIColor redColor];
        _scrollView.showsVerticalScrollIndicator = YES;
        _scrollView.showsHorizontalScrollIndicator = YES;
    }
    
    return _scrollView;
}

- (UIView *)containerView{

    if (_containerView==nil) {
        
        _containerView = [[UIView alloc] init];
        _containerView.backgroundColor = [UIColor orangeColor];
    }
    
    return _containerView;
}

上一篇 下一篇

猜你喜欢

热点阅读