横屏模式(landscape)下的UIDatePicker

简介:

ios的UIDatePicker控件在默认情况下,通常在竖屏模式下会显示得很好,但是在横屏模式下就会出现错位得情况。

要解决该问题可以在对应得视图控制器中加入下面得代码:

 
  1. - (void) viewDidLoad { 
  2.     [super viewDidLoad]; 
  3.     for (UIView * subview in datePicker.subviews) { 
  4.         subview.frame = datePicker.bounds; 
  5.     } 

然后在测试显示就不会错位了,如下:

比未作任何处理之前好多了,至少可以正常的显示了,实际上我们做的操作就是改变了datapicker的frame属性。

因此,我们可以完成一个可以旋转的UIDatePicker的子类来让UIDatePicker支持横屏和竖屏,代码如下:

RotatingDatePicker.h

 
  1. #import <UIKit/UIKit.h> 
  2. @interface RotatingDatePicker : UIDatePicker { 
  3. @end 

RotatingDatePicker.m

 
  1. #import "RotatingDatePicker.h" 
  2.  
  3. @implementation RotatingDatePicker 
  4.  
  5. - (id)initWithFrame: (CGRect)frame { 
  6.     if (self = [super initWithFrame:frame]) { 
  7.         for (UIView * subview in self.subviews) { 
  8.             subview.frame = self.bounds; 
  9.         } 
  10.     } 
  11.     return self; 
  12.  
  13. - (id) initWithCoder: (NSCoder *)aDecoder { 
  14.     if (self = [super initWithCoder: aDecoder]) { 
  15.         for (UIView * subview in self.subviews) { 
  16.             subview.frame = self.bounds; 
  17.         } 
  18.     } 
  19.     return self; 
  20.  
  21. @end 

现在,当要创建UIDatePicker或者使用ib来做界面事就都可以直接使用RotatingDatePicke来让UIDatePicker支持旋转。

还有一个问题就是UIDatePicker在横屏模式下并不会横向完全填充,但是我们可以通过代码手动将其修正。

 
  1. - (void) arrangeViews: (UIInterfaceOrientation)orientation { 
  2.     if (UIInterfaceOrientationIsPortrait(orientation)) { 
  3.         datePicker.frame = CGRectMake(0, 0, 320, 216); 
  4.     } 
  5.     else { 
  6.         datePicker.frame = CGRectMake(0, 0, 480, 162); 
  7.     } 
  8.  
  9. - (void) viewWillAppear:(BOOL)animated { 
  10.     [super viewWillAppear: animated]; 
  11.     [self arrangeViews: 
  12.           [UIApplication sharedApplication].statusBarOrientation]; 
  13.  
  14. - (void) willAnimateRotationToInterfaceOrientation: 
  15.                           (UIInterfaceOrientation)interfaceOrientation 
  16.          duration: (NSTimeInterval)duration { 
  17.     [super willAnimateRotationToInterfaceOrientation: interfaceOrientation 
  18.                                             duration: duration]; 
  19.     [self arrangeViews: interfaceOrientation]; 

通过上面的代码,使得在旋转时根据UIInterfaceOrientation方向

重新设置frame属性,使得在横屏模式下只显示三行信息,如下下图所示:

参考连接:http://www.llamagraphics.com/developer/using-uidatepicker-landscape-mode




本文转自 yarin 51CTO博客,原文链接:http://blog.51cto.com/yarin/574531,如需转载请自行联系原作者

相关文章
|
3月前
|
前端开发 小程序
监听手机屏幕旋转 横屏 竖屏 dome
监听手机屏幕旋转 横屏 竖屏 dome
|
8月前
|
Swift iOS开发
iOS PhotoBrowser 横竖屏图片浏览器
iOS PhotoBrowser 横竖屏图片浏览器
103 0
|
API iOS开发 Perl
iOS UIButton倒计时、指示器、粒子效果
iOS UIButton倒计时、指示器、粒子效果
iOS UIButton倒计时、指示器、粒子效果
|
存储 iOS开发
iOS流布局UICollectionView系列五——圆环布局的实现
iOS流布局UICollectionView系列五——圆环布局的实现
222 0
iOS流布局UICollectionView系列五——圆环布局的实现
|
iOS开发
iOS流布局UICollectionView系列四——自定义FlowLayout进行瀑布流布局(二)
iOS流布局UICollectionView系列四——自定义FlowLayout进行瀑布流布局
190 0
|
iOS开发
iOS流布局UICollectionView系列四——自定义FlowLayout进行瀑布流布局(一)
iOS流布局UICollectionView系列四——自定义FlowLayout进行瀑布流布局
334 0
iOS流布局UICollectionView系列四——自定义FlowLayout进行瀑布流布局(一)