autolayout - sizeClass - 4
2015-06-30 22:25
183 查看
经过上面几篇大牛的博客我想大概得肯定知道什么时候该用autoLayout,什么时候该用frame code的布局了吧
其中一点就是iPhone和iPad的共适配的时候该用AL布局,而之所以要用无非就是因为iPad的屏幕横竖转屏造成的界面元素的重新布局:
这里有一篇非常优秀的文章:http://blog.csdn.net/liangliang103377/article/details/40082271 可作为参考。
而其中提到的一个方法,尤其重要:
下面原搬大牛的例子:
当你的设备转换屏幕的时候,iOS就会自动添加约束并且是伴随着动画效果的哦。
注:转自:http://blog.csdn.net/liangliang103377/article/details/40082271
待续...
其中一点就是iPhone和iPad的共适配的时候该用AL布局,而之所以要用无非就是因为iPad的屏幕横竖转屏造成的界面元素的重新布局:
这里有一篇非常优秀的文章:http://blog.csdn.net/liangliang103377/article/details/40082271 可作为参考。
而其中提到的一个方法,尤其重要:
/* This method is called when the view controller's trait collection is changed by its parent. If you override this method, you should either call super to propagate the change to children or manually forward the change to children. */ - (void)willTransitionToTraitCollection:(UITraitCollection *)newCollection withTransitionCoordinator:(id <UIViewControllerTransitionCoordinator>)coordinator NS_***AILABLE_IOS(8_0);
下面原搬大牛的例子:
@interface AutoLayoutPracController () { UIView* greenView; UIView* yellowView; UIView* blueView; } @end
- (void)viewDidLoad { [super viewDidLoad]; [self.view setBackgroundColor:[UIColor lightGrayColor]]; self.view.translatesAutoresizingMaskIntoConstraints = NO; greenView = [self alView]; greenView.backgroundColor = [UIColor greenColor]; [self.view addSubview:greenView]; yellowView = [self alView]; yellowView.backgroundColor = [UIColor yellowColor]; [self.view addSubview:yellowView]; blueView = [self alView]; blueView.backgroundColor = [UIColor blueColor]; [self.view addSubview:blueView]; } //量产UIView - (UIView*)alView { UIView* newView = [UIView new]; newView.translatesAutoresizingMaskIntoConstraints = NO; return newView; } //真正上这片博客与别的博客的不一样!
//竖屏的约束 , 要注意的是,当作为参数传入局部变量的时候,VFL语句的名字也要和参数名字相同 : greenView_ @"[greenView_]" - (NSMutableArray*)portraitConstraintsWithGreenView:(UIView*)greenView_ yellowView:(UIView*)yellowView_ blueView:(UIView*)blueView_ { NSMutableArray* constraintArray = [NSMutableArray array]; [constraintArray addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-20-[greenView_]-20-[yellowView_(==greenView_)]-20-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(greenView_, yellowView_)]]; [constraintArray addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-20-[greenView_]-20-[blueView_(==greenView_)]-20-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(greenView_, blueView_)]]; [constraintArray addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-20-[yellowView_]-20-[blueView_(==yellowView_)]-20-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(yellowView_, blueView_)]]; [constraintArray addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-20-[blueView_]-20-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(blueView_)]]; return constraintArray; } //横屏的操作 - (NSMutableArray*)landscapeConstraintsWithGreenView:(UIView*)greenView_ yellowView:(UIView*)yellowView_ blueView:(UIView*)blueView_ { NSMutableArray* constraintArray = [NSMutableArray array]; [constraintArray addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-20-[greenView_]-20-[yellowView_(==greenView_)]-20-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(greenView_, yellowView_)]]; [constraintArray addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-20-[blueView_]-20-[greenView_(==blueView_)]-20-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(greenView_, blueView_)]]; [constraintArray addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-20-[blueView_]-20-[yellowView_(==blueView_)]-20-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(blueView_, yellowView_)]]; [constraintArray addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-20-[blueView_]-20-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(blueView_)]]; return constraintArray; }
/** 本篇文章最值钱就是这里了,横竖屏幕的转化 **/ - (void)willTransitionToTraitCollection:(UITraitCollection *)newCollection withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator { [super willTransitionToTraitCollection:newCollection withTransitionCoordinator:coordinator]; [coordinator animateAlongsideTransition:^(id <UIViewControllerTransitionCoordinatorContext> context){ if (newCollection.verticalSizeClass == UIUserInterfaceSizeClassCompact) { NSLog(@"%s --- %d", __FUNCTION__, __LINE__); [self.view removeConstraints:self.view.constraints]; [self.view addConstraints:[self landscapeConstraintsWithGreenView:greenView yellowView:yellowView blueView:blueView]]; } else { NSLog(@"%s --- %d", __FUNCTION__, __LINE__); [self.view removeConstraints:self.view.constraints]; [self.view addConstraints:[self portraitConstraintsWithGreenView:greenView yellowView:yellowView blueView:blueView]]; } } completion:nil]; } @end
当你的设备转换屏幕的时候,iOS就会自动添加约束并且是伴随着动画效果的哦。
注:转自:http://blog.csdn.net/liangliang103377/article/details/40082271
待续...
相关文章推荐
- c++中的using namespace std
- WCF回顾一、基本概念和应用场景
- Linux进程间通信(六)---信号量通信之semget()、semctl()、semop()及其基础实验
- 全局变量和全局静态变量的区别
- libxml2 的一些用法
- 【再思考】排序算法——归并排序
- 数组
- spring+struts+hibernate
- 【Android】组件生命周期
- MVC几种传值方式
- 网络编程
- 泛型和面向对象C++
- 深入浅出JMS(一)--JMS基本概念
- java自带的MD5加密 以及jsMD5加密
- debian命令行安装sqlite3
- 如何理解: java的import ???
- NSDictionary和NSMutableDictionary的相关总结
- 深入浅出JMS(一)--JMS基本概念
- WSGI的理解
- TFS 2015新功能之一,当前迭代查询标记