最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

给Your Second iOS App的BirdWatching添加屏幕旋转(orientation rotation)的支持

iOS crifan 2223浏览 0评论

之前已经参考:Your Second iOS App完成了StoryBoard的BirdWatching的app

现在继续根据其中Your Second iOS App – Next Steps的建议去给其添加屏幕旋转的支持。

1.另外注意到,默认的view的orientation是Inferred:

default orientation inferred

然后尝试将其改为Landscape:

orientation landscape

很明显,所有的view都变成横向的了。

然后又改回了原先的Inferred。因为暂时不清楚Inferred是啥意思。

2.查了下单词Inferred的,是”adj. 推论的;推测出的“的意思。

所以,应该是根据当前设备的方向,决定当view的方向的。

3.然后参考了:

《Iphone开发基础教程》第五章 自动旋转和调整大小

去启动iOS模拟器,然后试了试Command+->,就可以分别实现四种方向的切换了:

iOS Protrait

 

iOS LandscapeLeft

 

iOS PortraitUpsideDown

iOS LandscapeRight

对应的,如果是按Command+-<就是向左,反方向去转动iPhone的效果了。

4.去根据教程提示,先去学习shouldAutorotateToInterfaceOrientation,看了解释后知道了:

其中所支持的方向,是定义在UIInterfaceOrientation中的,其中有四种:

UIDeviceOrientationPortrait

UIDeviceOrientationPortraitUpsideDown

UIDeviceOrientationLandscapeRight

UIDeviceOrientationLandscapeLeft

而且,还想起来了,好像之前在代码中看到过这个函数的,所以就去搜索整个项目,找到了三个:

shouldAutorotateToInterfaceOrientation find result

因此,想要支持各种屏幕方向,最简单的,就是参考:

shouldAutorotateToInterfaceOrientation doesn’t work

在函数shouldAutorotateToInterfaceOrientation中,写出:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { 
 return YES; 
}  

或:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { 
 return (interfaceOrientation == UIInterfaceOrientationPortrait || 
   interfaceOrientation == UIInterfaceOrientationLandscapeLeft ||  
   interfaceOrientation == UIInterfaceOrientationLandscapeRight || 
   interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown ); 
}  

即可。

此处先去使用第一种,把三处都改为YES试试。

然后结果是显示的内容,都可以根据屏幕自动旋转的。

对应的,又把其中MasterView中的改为原先的代码,此时,测试结果表明,如代码所设置的,是不支持PortraitUpsideDown的:

not support portraitupsidedown

然后再确保把三处都改为YES,然后之前的设置保持Orientation为Inferred,这样就都可以自动支持屏幕旋转了。

目前唯一一点不是很爽的是,由于Table View中的textFiled都是static,固定设置好了宽度的,导致在竖屏显示的时候,输入框的比例很合适:

portrait input box ok

但是切换到横屏后,输入框就没法自动适应横向宽度,显得很短:

input box short

而如果去手动把add scene中输入框去手动调节,但是只能先设置为某种方向,比如Landscape后,再手动调节:

show textfield not well

但是此时调节出来的宽度,也只是针对landscape,切换到portrait后,还会是太长或太短。总之没有自适应的。

貌似需要将其变成动态的cell,然后用代码实现动态匹配宽度?

还是写代码实现,在转屏后,加上对应代码调节输入框宽度?

【总结】

无论如何,目前暂时是对于支持屏幕旋转是支持了。

【仍存问题】

在转屏后,之前所固定设计的输入框等所显示的内容的宽度,没法自适应屏幕的长宽,导致转屏后,显示出来效果不是很好,需要改进。


后来继续折腾,解决了屏幕切换时,TextField宽度调整的问题:

【已解决】iOS中,在屏幕转换/切换时,如何实现自动或手动调整TextField的宽度

转载请注明:在路上 » 给Your Second iOS App的BirdWatching添加屏幕旋转(orientation rotation)的支持

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

网友最新评论 (1)

    86 queries in 0.185 seconds, using 22.16MB memory