2014-04-27 58 views
1

我想在用戶單擊按鈕時將UIDatePicker添加到UIView。我有一個動畫設置,從底部向上滑動UIView。如果我將UIDatePicker添加到ViewControllers視圖,則DatePicker將正確顯示。但是,如果我將DatePicker添加到UIView,那麼日期選取器根本不顯示。在運行時將UIDatePicker添加到UIView的問題

- (IBAction)setTimeButtonPressed:(UIButton *)sender 
{ 
    // Creates Frame Rects for views and controls 
    CGRect datePickerViewOutFrame = CGRectMake(0, 336, self.view.frame.size.width, 228); 
    CGRect datePickerOutFrame = CGRectMake(0, 336, self.view.frame.size.width, 162); 

    // Create View that will slide out 
    UIView *datePickerView = [[UIView alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height, self.view.frame.size.width, 0)]; 
    datePickerView.backgroundColor = [UIColor flatCloudsColor]; 

    // Setup DatePicker 
    UIDatePicker *datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height, self.view.frame.size.width, 0)]; 
    datePicker.datePickerMode = UIDatePickerModeTime; 
    datePicker.date = [NSDate date]; 
    datePicker.hidden = NO; 

    [self.view addSubview:datePickerView]; 
    [datePickerView addSubview:datePicker]; /* DOES NOT WORK*/ 

    self.datePickerView.clipsToBounds = YES; 

    // Animate the views sliding out from the bottom 
    [UIView animateWithDuration:0.5 
          delay:0 
         options:UIViewAnimationOptionCurveLinear 
        animations:^{ 
         datePicker.frame = datePickerOutFrame; 
         datePickerView.frame = datePickerViewOutFrame; 

        } 
        completion:^(BOOL finished){ 
         // Might need later 
        }]; 
} 

但是,如果我改變,我添加日期選擇器的線路,它的工作原理,但然後我不能把它夾到datePickerView的邊界。

[self.view addSubview:datePickerView]; 
[self.view addSubview:datePicker]; /* Works but can't clip DatePicker */ 

我在做什麼錯?

回答

1

UIDatePickerView框架需要相對於其父視圖。因此,如果您想將其添加到您的datePickerView,那麼UIDatePicker的幀應該具有0,0的原點,而不是基於視圖控制器視圖的高度。

UIDatePicker *datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 0)]; 
+0

啊,我知道我錯過了一些東西。謝謝,那就是訣竅! –