iOS 开发

viewWillLayoutSubviews和通知的使用机制

2016-07-26  本文已影响647人  不会打滚儿的狮子

做横竖屏最重要的是确定横竖屏响应的接口。目前我知道的有两种方式 :

1.使用通知。

}

-(void)_orientationDidChange:(NSNotification*)notify
{
[self _shouldRotateToOrientation:(UIDeviceOrientation)[UIApplication sharedApplication].statusBarOrientation];
}

-(void)_shouldRotateToOrientation:(UIDeviceOrientation)orientation {
if (orientation == UIDeviceOrientationPortrait ||orientation == UIDeviceOrientationPortraitUpsideDown) {
// 竖屏
}else {
// 横屏
}
}
上述代码,一看就明白。
2.使用 viewWillLayoutSubviews
测试发现横竖屏切换的时候,系统会响应一些函数,其中 viewWillLayoutSubviews就是之一。

上一篇 下一篇

猜你喜欢

热点阅读