UIDatePicker提升tableview内容

时间:2014-05-08 15:12:01

标签: ios uidatepicker

我有一个带有导航栏的UIViewController,还有一个UITableView。我在视图控制器中有一个按钮,按下该按钮会弹出UIDatePicker。但是,当我按下按钮弹出UIDatePicker时,桌面视图中的所有内容都会向上移动大约30个像素。不酷。我希望UIDatePicker只是弹出超过视图控制器而不移动任何元素。这是我的日期选择器的代码。想法?

- (IBAction)timeWasPressed:(id)sender {

if(self.pickerShown == NO){
    self.slidePicker = [[UIDatePicker alloc] init];
    self.slidePicker.datePickerMode = UIDatePickerModeTime;
    self.slidePicker.backgroundColor = [UIColor whiteColor];
    [self.navigationController setNavigationBarHidden:YES];


    [self.view addSubview:self.slidePicker];

    //make done button
    self.toolBar= [[UIToolbar alloc] initWithFrame:CGRectMake(0,0,320,64)];
    self.toolBar.barTintColor = [UIColor whiteColor];
    UIBarButtonItem *barButtonDone = [[UIBarButtonItem alloc] initWithTitle:@"Done"
                                                                        style:UIBarButtonItemStylePlain target:self
                                                                     action:@selector(doneButton)];

    self.toolBar.items = [[NSArray alloc] initWithObjects:barButtonDone,nil];
    barButtonDone.tintColor=[UIColor blackColor];
    self.pickerShown = YES;
    [self.view addSubview:self.toolBar];
}
 }

-(void)doneButton{

NSDate *pickerDate = [self.slidePicker date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateStyle:NSDateFormatterNoStyle];
[dateFormatter setTimeStyle:NSDateFormatterShortStyle];

NSString *selectionString = [[NSString alloc] initWithFormat:@"%@", [dateFormatter    stringFromDate:pickerDate]]; //
[self.timeButton setTitle:selectionString forState:UIControlStateNormal];

self.pickerShown = NO;

[self.slidePicker removeFromSuperview];
[self.toolBar removeFromSuperview];
[self.navigationController setNavigationBarHidden:NO];

}

2 个答案:

答案 0 :(得分:0)

我认为一切都向上移动的原因是因为你隐藏了导航栏。

[self.navigationController setNavigationBarHidden:YES];

用以下内容替换该行,如果您看到所有内容都是动画,那么这就是一切都在变化的原因。

[self.navigationController setNavigationBarHidden:true animated:true];

如果您希望日期选择器显示在导航控制器上,而不是将其添加到视图控制器的视图中,请将选择器添加到导航控制器的视图中。

所以..替换

[self.view addSubview:self.slidePicker];

与..

[self.navigationController.view addSubview:self.slidePicker];

希望它成功。

答案 1 :(得分:0)

杰森说的是对的,但他忘了说你必须抹掉这条线

[self.navigationController setNavigationBarHidden:YES];

所以,你必须更换

//....
[self.view addSubview:self.slidePicker];
//...
[self.view addSubview:self.toolBar];
//...

//...
[self.navigationController.view addSubview:self.slidePicker];
//...
[self.navigationController.view addSubview:self.toolBar];
//...

所以你的“timeWasPressed:”代码将是:

self.slidePicker = [[UIDatePicker alloc] init];
self.slidePicker.datePickerMode = UIDatePickerModeTime;
self.slidePicker.backgroundColor = [UIColor whiteColor];
//[self.navigationController setNavigationBarHidden:YES];

[self.navigationController.view addSubview:self.slidePicker];

//make done button
self.toolBar= [[UIToolbar alloc] initWithFrame:CGRectMake(0,0,320,64)];
self.toolBar.barTintColor = [UIColor whiteColor];
UIBarButtonItem *barButtonDone = [[UIBarButtonItem alloc] initWithTitle:@"Done"
                                                                  style:UIBarButtonItemStylePlain target:self
                                                                 action:@selector(doneButton)];

self.toolBar.items = [[NSArray alloc] initWithObjects:barButtonDone,nil];
barButtonDone.tintColor=[UIColor blackColor];
self.pickerShown = YES;
[self.navigationController.view addSubview:self.toolBar];

别忘了你也可以删除

[self.navigationController setNavigationBarHidden:NO];

来自你的doneButton方法。

希望有所帮助。