iOS成长之AutoLayout写控件的注意事项
2016-01-07 17:22
387 查看
本篇博客主要总结,在使用AutoLayout写控件时的注意事项。
Step 1,重写+ (BOOL)requiresConstraintBasedLayout; 方法,返回YES。这样默认就是使用自动布局。
Step 2,组件位置,样式变化等
1. 重写- (void)updateConstraints;组件位置,大小的变化在这个方法里面写。
触发方法
下面是代码事例。摘自Masonry iOS Examples
Step 1,重写+ (BOOL)requiresConstraintBasedLayout; 方法,返回YES。这样默认就是使用自动布局。
- (BOOL)requiresConstraintBasedLayout { return YES; }
Step 2,组件位置,样式变化等
1. 重写- (void)updateConstraints;组件位置,大小的变化在这个方法里面写。
- (void)updateConstraints { 重写对view写约束 //according to apple super should be called at end of method [super updateConstraints]; }
触发方法
- (void)toggleButtonPosition { // tell constraints they need updating [self setNeedsUpdateConstraints]; // update constraints now so we can animate the change [self updateConstraintsIfNeeded]; // 使用UIView animateWithDuration 实现动画的效果 [UIView animateWithDuration:0.4 animations:^{ [self layoutIfNeeded]; }]; }
下面是代码事例。摘自Masonry iOS Examples
// // MASExampleRemakeView.m // Masonry iOS Examples // // Created by Sam Symons on 2014-06-22. // Copyright (c) 2014 Jonas Budelmann. All rights reserved. // #import "MASExampleRemakeView.h" @interface MASExampleRemakeView () @property (nonatomic, strong) UIButton *movingButton; @property (nonatomic, assign) BOOL topLeft; - (void)toggleButtonPosition; @end @implementation MASExampleRemakeView - (id)init { self = [super init]; if (!self) return nil; self.movingButton = [UIButton buttonWithType:UIButtonTypeSystem]; [self.movingButton setTitle:@"Move Me!" forState:UIControlStateNormal]; self.movingButton.layer.borderColor = UIColor.greenColor.CGColor; self.movingButton.layer.borderWidth = 3; [self.movingButton addTarget:self action:@selector(toggleButtonPosition) forControlEvents:UIControlEventTouchUpInside]; [self addSubview:self.movingButton]; self.topLeft = YES; return self; } + (BOOL)requiresConstraintBasedLayout { return YES; } // this is Apple's recommended place for adding/updating constraints - (void)updateConstraints { [self.movingButton remakeConstraints:^(MASConstraintMaker *make) { make.width.equalTo(@(100)); make.height.equalTo(@(100)); if (self.topLeft) { make.left.equalTo(self.left).with.offset(10); make.top.equalTo(self.top).with.offset(10); } else { make.bottom.equalTo(self.bottom).with.offset(-10); make.right.equalTo(self.right).with.offset(-10); } }]; //according to apple super should be called at end of method [super updateConstraints]; } - (void)toggleButtonPosition { self.topLeft = !self.topLeft; // tell constraints they need updating [self setNeedsUpdateConstraints]; // update constraints now so we can animate the change [self updateConstraintsIfNeeded]; [UIView animateWithDuration:0.4 animations:^{ [self layoutIfNeeded]; }]; } @end
相关文章推荐
- ios测试基础七:常用软件
- iOS多线程中的dispatch_semaphore_t
- iOS 开发笔记
- IOS-添加分段控件SegmentControl
- 2016最新iOS开发者证书申请及安装教程
- IOS 让tableview选中效果渐变消失
- iOS崩溃调试
- iOS下int long longlong的取值范围
- IOS Replaykit 手游录屏Demo
- iOS 实现录音并保存在指定文件目录下面
- iOS 集合的深复制与浅复制
- IOS应用在iPhone5和iPhone5s上不能全屏显示,应用画面上下各有1条黑色的解决方案
- iOS 数据持久化的几种方式
- 动态适应label的高度
- iOS 键盘遮挡问题
- iOS中,如何做到未知参数数量的反射
- 转载自简书iOS 异步绘制相关
- iOS json字段转属性
- iOS基础——浅谈个人对协议、代理的理解
- iOS -- NSDictionary 两种初始化方式的区别