屏幕旋转的处理方法,实现视图位置的变化

1.首先在自定义的视图中重写layoutSubviews方法

- (void)layoutSubviews{

  UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation ;//获取屏幕的方向,和状态栏是相同的

if (orientation  == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight) {//横向时的位置

self.button.frame = CGRectMake (300,150,200,40);

} else {

  self.button.frame = CGRectMake(150,150,100,40);

  }

}

2.在viewController.m文件中

- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<

UIViewControllerTransitionCoordinator

>)coordinator{

  [self.textField resignFirstResponder];//当屏幕尺寸改变时回收键盘

}

//设置屏幕当前方向

- (NSUInteger)supportedInterfaceOrientation{

  return UIInterfaceOrientationMaskAll;

}

原文地址:https://www.cnblogs.com/arenouba/p/5183299.html