横屏模式(landscape)下的UIDatePicker

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


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

- (void) viewDidLoad { 
    [super viewDidLoad]; 
    for (UIView * subview in datePicker.subviews) { 
        subview.frame = datePicker.bounds; 
    } 
}
然后在测试显示就不会错位了,如下:

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

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

RotatingDatePicker.h
#import <UIKit/UIKit.h> 
@interface RotatingDatePicker : UIDatePicker { 
} 
@end 
RotatingDatePicker.m
#import "RotatingDatePicker.h" 
 
@implementation RotatingDatePicker 
 
- (id)initWithFrame: (CGRect)frame { 
    if (self = [super initWithFrame:frame]) { 
        for (UIView * subview in self.subviews) { 
            subview.frame = self.bounds; 
        } 
    } 
    return self; 
} 
 
- (id) initWithCoder: (NSCoder *)aDecoder { 
    if (self = [super initWithCoder: aDecoder]) { 
        for (UIView * subview in self.subviews) { 
            subview.frame = self.bounds; 
        } 
    } 
    return self; 
} 
 
@end

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

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

- (void) arrangeViews: (UIInterfaceOrientation)orientation { 
    if (UIInterfaceOrientationIsPortrait(orientation)) { 
        datePicker.frame = CGRectMake(0, 0, 320, 216); 
    } 
    else { 
        datePicker.frame = CGRectMake(0, 0, 480, 162); 
    } 
} 
 
- (void) viewWillAppear:(BOOL)animated { 
    [super viewWillAppear: animated]; 
    [self arrangeViews: 
          [UIApplication sharedApplication].statusBarOrientation]; 
} 
 
- (void) willAnimateRotationToInterfaceOrientation: 
                          (UIInterfaceOrientation)interfaceOrientation 
         duration: (NSTimeInterval)duration { 
    [super willAnimateRotationToInterfaceOrientation: interfaceOrientation 
                                            duration: duration]; 
    [self arrangeViews: interfaceOrientation]; 
}

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

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


参考连接:http://www.llamagraphics.com/developer/using-uidatepicker-landscape-mode
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值