iOS学习之自定义视图时,在屏幕发生旋转时触发重新布局方法

时间:2022-10-10 13:26:56

如果要对自定义的视图在屏幕旋转时重新布局,则在自定义视图中定义以下触发方法:

-(void)layoutSubviews {
[super layoutSubviews];
//1.获取到屏幕旋转的方向
UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
//2.根据屏幕旋转方向布局子视图
switch (orientation) {
//竖直方向
case UIDeviceOrientationPortrait:
//倒立
case UIDeviceOrientationPortraitUpsideDown:
{
CGRect fram = self.loginButton.frame;
fram.origin.x = kMarginTop_LoginButton;
fram.origin.y = kMarginTop_LoginButton;
self.loginButton.frame = fram;
}
break; //右横屏
case UIDeviceOrientationLandscapeRight:
//左横屏
case UIDeviceOrientationLandscapeLeft:
{
CGRect fram = self.loginButton.frame;
fram.origin.x = kMarginLeft_LoginButton_Landscape;
fram.origin.y = kMarginTop_DescripLabel;
self.loginButton.frame = fram;
}
break; default:
break;
}
}