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

本文介绍了如何在iOS应用中实现按钮的布局适配不同屏幕方向。通过在自定义视图中重写layoutSubviews方法来调整按钮位置,并在屏幕尺寸变化时回收键盘,确保用户体验。

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值