NSLayoutConstraint

1.Demo

/*What's wrong with NSLayoutConstraints?

Under the hood Auto Layout is a powerful and flexible way of organising and laying out your views. However creating constraints from code is verbose and not very descriptive. Imagine a simple example in which you want to have a view fill its superview but inset by 10 pixels on every side*/

//
UIView *superview = self.view;

UIView *baseView  = [[UIView alloc] initWithFrame:CGRectMake(0, 100, 100, 100)];
baseView.backgroundColor = [UIColor yellowColor];
[superview addSubview:baseView];

UIView *view1 = [[UIView alloc] init];
view1.translatesAutoresizingMaskIntoConstraints = NO;
view1.backgroundColor = [UIColor greenColor];
[superview addSubview:view1];

UIEdgeInsets padding = UIEdgeInsetsMake(100, 100, 100, 100);

[superview addConstraints:@[
                       //view1 constraints
[NSLayoutConstraint constraintWithItem:view1
                                                         attribute:NSLayoutAttributeTop
                                                         relatedBy:NSLayoutRelationEqual
                                                            toItem:superview
                                                         attribute:NSLayoutAttributeTop
                                                        multiplier:1.0
                                                          constant:padding.top],
                            
[NSLayoutConstraint constraintWithItem:view1
                                                         attribute:NSLayoutAttributeLeft
                                                         relatedBy:NSLayoutRelationEqual
                                                            toItem:superview
                                                         attribute:NSLayoutAttributeLeft
                                                        multiplier:1.0
                                                          constant:padding.left],
                            
[NSLayoutConstraint constraintWithItem:view1
                                                         attribute:NSLayoutAttributeBottom
                                                         relatedBy:NSLayoutRelationEqual
                                                            toItem:superview
                                                         attribute:NSLayoutAttributeBottom
                                                        multiplier:1.0
                                                          constant:-padding.bottom],
                            
[NSLayoutConstraint constraintWithItem:view1
                                                         attribute:NSLayoutAttributeRight
                                                         relatedBy:NSLayoutRelationEqual
                                                            toItem:superview
                                                         attribute:NSLayoutAttributeRight
                                                        multiplier:1
                                                          constant:-padding.right],
                            
                            ]];
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 概述 以前一直听说autoLayout,跟xib、storybord无缝结合使用,设置各种约束条件来达到适配的目的...
    guaker阅读 6,509评论 6赞 7
  • 张晓波在一片混沌中睁开沉重的眼皮,视线一开始无法聚焦,只能看到脑门儿上一盏明晃的白炽灯隐约的在他眼前散出光圈,一晃...
    贰_1d1a阅读 3,618评论 0赞 0
  • 《5月 提笔写作》链接目录 6月 心灵修行 目录(更新中) 关于马盘的争议(6月1日) 马克思盘学习入门笔记(6月...
    晚晚inna阅读 394评论 0赞 0
  • 本文图片来自网络 姨姥姥是姥姥的小妹妹,比姥姥小了整整10岁。然而在我看来,姨姥姥比姥姥老得多。 姨姥姥的家离姥姥...
    爱做美梦的懒猫阅读 638评论 0赞 24

友情链接更多精彩内容