首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何使用UIDatePicker?

如何使用UIDatePicker?
EN

Stack Overflow用户
提问于 2011-04-14 09:43:16
回答 2查看 9.4K关注 0票数 3

我在viewControler上有一个按钮,我想要调出一个UIDatePicker。我想我需要将它动画输入/输出。

到目前为止,我已经设置了按钮,并在我的IB中有了一个单独的视图中的UIDatePicker。我认为让它以模态的方式出现是最有意义的。

需要什么代码来链接UIDatePicker,以便通过单击按钮滑入视图,然后滑出?

谢谢。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-04-14 10:03:04

您希望保持对UIDatePicker的引用,然后可以使用UIView来设置datePicker传入和传出的动画。我将选择器包装在一个UIView中,这样您就可以添加一个隐藏按钮或相关的控件。

头文件中的

代码语言:javascript
运行
复制
@interface MyControllerWithSlidingDatePicker : UIViewController{

  //If you use Interface builder, use this
  IBOutlet IDatePicker *picker; 
  //If you aren't using IB, use this
  //UIDatePicker *picker; 

}

@property (nonatomic, retain) UIDatePicker *picker;

@end

和实现文件:

代码语言:javascript
运行
复制
#import "MyControllerWithSlidingDatePicker.h"

@implementation MyControllerWithSlidingDatePicker

  - (id) init{
    self = [super init];
    if(self){
       //If you use Interface Builder, skip this part.

       UIDatePicker *dp = [[UIDatePicker alloc] init];
       //Configure your picker

       self.picker = dp;

       [dp release];

    }

    return self;  
  }

 - (IBAction)slidePickerIn{
   //Position the picker out of site
   [self.picker setFrame:CGRectMake(0,self.view.frame.size.height,self.width,self.picker.frame.size.height];

   //Add the picker to the view
   [self.view addSubview:self.picker];

   //This animation will work on iOS 4
   //For older iOS, use "beginAnimation:context"
   [UIView animateWithDuration:1.0 animations:^{

      [self.datePicker setFrame:CGRectMake0,self.view.frame.size.height-self.datePicker.frame.size.height,self.datePicker.frame.size.width,self.datePicker.frame.size.height];

   }  

 }

 - (void)dealloc{
   [picker release];
   [super dealloc];
 }

@end

要制作动画,只需使用另一种方法将帧更改为原始的“屏幕外”帧即可。

票数 3
EN

Stack Overflow用户

发布于 2013-05-30 19:54:36

你可以省去这个麻烦,把你的UIDatePicker设置成你的UITextField的“键盘”。如下所示:

代码语言:javascript
运行
复制
UIDatePicker *datePicker = [UIDatePicker new];
self.dateField.inputView = datePicker;

这样,它的行为就像一个键盘,并根据需要进行动画输入和输出。您可以使用textField委托方法来通知它离开。简单的方法已经够难的了。这就是我的哲学;)

票数 16
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5657679

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档