iPad使用相机中的一个坑(UIModalPresentationStyle,UIStatusBar)

今天解决了一个相机相关的bug,感觉有些人可能也会遇到,就说一下吧。
问题是这样子的,我们应用在用户信息页面,需要支持更换头像,这里需要能够直接从相机获取,同样的代码,在iPhone上没有任何问题,切换到iPad以后,发现照相页面总是会出现statusBar,无论是重写prefersStatusBarHidden函数还是通过[[UIApplication sharedApplication] setStatusBarHidden:YES]进行设置都不生效,具体的现象可以看下图:

iPad使用相机中的一个坑(UIModalPresentationStyle,UIStatusBar)_第1张图片
现象.jpg

后来各种查文档,找资料,才发现是由于用户信息页面是通过UIModalPresentationStyleFormSheet方式present导致的问题,这样present出来的页面对statusBar的设置都是无效的。必须设置controllermodalPresentationCapturesStatusBarAppearance属性才可以。
设置以后,新的页面对statusBar的设置才会有效。

When you present a view controller by calling the present(_:animated:completion:)
method, status bar appearance control is transferred from the presenting to the presented view controller only if the presented controller'��s modalPresentationStyle
value is fullScreen
. By setting this property to true
, you specify the presented view controller controls status bar appearance, even though presented non-fullscreen.
The system ignores this property’s value for a view controller presented fullscreen.

文档中描述了,默认只有全屏模式的presentedController才会转换statusBar的控制权,否则statusBar仍旧由前一个页面控制。如果想要present一个非全屏的页面,又要控制statusBar,那么就要把这个属性设置为true

苹果的开发文档中对这个属性已经做了描述,但却由于不经常使用被我和同时都给忽略了,才会出现这样的问题,希望我们读文档时能够细心一点,避免再次出现这样的问题。

你可能感兴趣的:(iPad使用相机中的一个坑(UIModalPresentationStyle,UIStatusBar))