Autorotation and Autosizing
配置应用级别的旋转方向——Global Setting
方法:点击项目-General-Deployment-Device Orientation
It doesn’t necessarily mean that every view in your application will use all of the selected orientations; but if you’re going to support an orientation in any of your application’s views, that orientation must be selected here.
配置视图级别的旋转方向——Local Setting
方法:ViewController.m中
- (NSUInteger)supportedInterfaceOrientations
{
return (UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft);
}
同一个App的不同视图,旋转方向的限制可能不一样。只能在应用级别的旋转方向上限制更多,比如应用级别不支持Upside-down,视图级别也一定不行。
附,旋转方向:
- UIInterfaceOrientationMaskPortrait
- UIInterfaceOrientationMaskLandscapeLeft
- UIInterfaceOrientationMaskLandscapeRight
- UIInterfaceOrientationMaskPortraitUpsideDown
- UIInterfaceOrientationMaskLandscape
- UIInterfaceOrientationMaskAll
- UIInterfaceOrientationMaskAllButUpsideDown
旋转,横竖视图排版一样
添加四个角(1,2,5,6)的Label
全选四个角(1,2,5,6)的Label,选择Editor -> Resolve Auto Layout Issues -> Add Missing Constraints
添加中间(3,4)的Label,对齐
左中(3)Editor -> Align -> Vertical Center in Container
右中(4)Editor -> Resolve Auto Layout Issues -> Add Missing Constraints
(1,2)加背景颜色,调整它们的宽度(随便,不一定等宽)
(1,2) Editor -> Pin -> Horizontal Spacing(无论怎么旋转都不等宽)
(1,2) Editor -> Pin -> Widths Equally(无论怎么旋转都等宽)
旋转,横竖视图排版不一样
创建SingleViewApplication,在View的File Inspector中取消AutoLayout,我们要利用代码实现布局。
在Main.storyboard中拖入一个View和4个Button,并设置该View的背景颜色。
ViewController.m中实现代码
旋转的时候就自动调用
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
[self adjustLayout:toInterfaceOrientation];
}
- (void)adjustLayout:(UIInterfaceOrientation)toInterfaceOrientation
{
if (UIInterfaceOrientationIsPortrait(toInterfaceOrientation))
{
[self layoutPortrait];
}
else
{
[self layoutLandscape];
}
}
static const CGFloat btnHeight = ;
static const CGFloat btnWidth = ;
static const CGFloat spacing = ; - (void)layoutPortrait
{
CGRect b = self.view.bounds;
CGFloat contentHeight = CGRectGetHeight(b)-btnHeight*-spacing*;
CGFloat contentWidth = CGRectGetWidth(b) - spacing*;
self.contentView.frame = CGRectMake(spacing, spacing, contentWidth, contentHeight); CGFloat btn1x = spacing;
CGFloat btn2x = CGRectGetWidth(b)-spacing-btnWidth;
CGFloat btn1y = contentHeight+*spacing;
CGFloat btn2y = btn1y+btnHeight+spacing; self.btn1.frame = CGRectMake(btn1x, btn1y, btnWidth, btnHeight);
self.btn2.frame = CGRectMake(btn2x, btn1y, btnWidth, btnHeight);
self.btn3.frame = CGRectMake(btn1x, btn2y, btnWidth, btnHeight);
self.btn4.frame = CGRectMake(btn2x, btn2y, btnWidth, btnHeight);
} - (void)layoutLandscape
{
CGRect b = self.view.bounds;
CGFloat contentHeight = CGRectGetHeight(b)-spacing*;
CGFloat contentWidth = CGRectGetWidth(b) - spacing*-btnWidth;
self.contentView.frame = CGRectMake(spacing, spacing, contentWidth, contentHeight); CGFloat btnx = CGRectGetWidth(b)-spacing-btnWidth;
CGFloat btny1 = spacing;
CGFloat btny4 = CGRectGetHeight(b)-spacing-btnHeight;
CGFloat btny2 = btny1+(btny4-btny1)/3.0;
CGFloat btny3 = btny2+(btny4-btny1)/3.0; self.btn1.frame = CGRectMake(btnx, btny1, btnWidth, btnHeight);
self.btn2.frame = CGRectMake(btnx, btny2, btnWidth, btnHeight);
self.btn3.frame = CGRectMake(btnx, btny3, btnWidth, btnHeight);
self.btn4.frame = CGRectMake(btnx, btny4, btnWidth, btnHeight);
}
viewDidLoad只在程序启动时调用了一次;屏幕旋转的时候没有调用它,调用的是- (void)willAnimateRotationToInterfaceOrientation:duration:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//处理启动时不是默认竖屏的情况。
UIApplication *app = [UIApplication sharedApplication];
UIInterfaceOrientation currentOrientation = app.statusBarOrientation;
[self adjustLayout:currentOrientation];
}
效果
最新文章
- 你还可以再诡异点吗——SQL日志文件不断增长
- 【WCF】终结点的监听地址
- ubantu16.04+mxnet +opencv+cuda8.0 环境搭建
- Roguelike 相关知识
- navicat连接虚拟机(centos)中的mysql
- linux基础-第十三单元 硬盘分区、格式化及文件系统的管理二
- Ubuntu下SVN配置
- 21.C#序列过虑、排序、let子句和连接(十一章11.3-11.5)
- C++运算符重载——重载特殊运算符
- 不惧面试:HTTP协议(3) - Cookie
- 安装node配置环境变量,解决某组件(如cordova,webpack等)“不是内部命令”问题
- linux上的组管理
- 在C++98基础上学习C++11新特性
- 如何才能成为一个合格的web前端工程师
- sql片段
- QT上位机
- [转] C++ 和 python之间的互相调用
- SQLServer 查看耗时较多的SQL语句(转)
- 【API】注册表编程基础-RegCreateKeyEx、RegSetValueEx
- Java Web(十一) 分页功能的实现