0

我创建了一个文本字段,其第一响应者是 datePicker。我还在视图上附加了一个工具栏,因此当点击文本字段时,工具栏会随着动画向上滑动。然后在工具栏上,他们是一个完成按钮,可以让第一响应者辞职。我也希望它删除工具栏。为此,我添加了这个

[pickerBar removeFromSuperview];

然后重新打开它我这样做了

[self.view addSubview:pickerBar];

因此,它在触摸 textview 时被激活。

问题是当我再次点击文本字段时,工具栏会失去导航。

这是大部分代码

pickerBar = [[UIToolbar alloc] init];
        pickerBar.barStyle=UIBarStyleBlackOpaque;
        [pickerBar sizeToFit];

        if([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone && [UIScreen mainScreen].bounds.size.height == 568.0) {
            CGRect pickerBarRect = CGRectMake(0, 568, 320, 44);
            [pickerBar setFrame:pickerBarRect];
        } else {
            CGRect pickerBarRect = CGRectMake(0, 480, 320, 44);
            [pickerBar setFrame:pickerBarRect];
        }

        //Set up the new position of the frame of the view

        NSMutableArray *barItems = [[NSMutableArray alloc] init];

        UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
        [barItems addObject:flexSpace];

        UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(releasePicker)];
        [barItems addObject:doneBtn];

        [barItems addObject:doneBtn];
        [pickerBar setItems:barItems animated:YES];


        }
    }
}
- (void)releasePicker {
    [textfield resignFirstResponder];

    [pickerBar removeFromSuperview];
}

- (void)pickerActivated {

    [UIView animateWithDuration:.4 animations:^(void) {

    [self.view addSubview:pickerBar];



        if([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone && [UIScreen mainScreen].bounds.size.height == 568.0){

            CGRect rect = CGRectMake(0, 266, 320, 44);

            [pickerBar setFrame:rect];

            pickerBar.hidden = NO;

        } else {


            CGRect rect = CGRectMake(0, 178, 320, 44);

            [pickerBar setFrame:rect];




        }
}];

}
4

2 回答 2

2

inputView和之间有区别inputAccessoryView。这就是你想要的:

// setting the inputView
UIPickerView *pickerView = [[UIPickerView alloc] init];
// other configurations //
self.textField.inputView = pickerView;

// setting up the inputAccessoryView
UIToolbar *pickerBar = [[UIToolbar alloc] init];
pickerBar.barStyle = UIBarStyleBlackOpaque;
[pickerBar sizeToFit];

UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace 
                                                                           target:self 
                                                                           action:nil];
UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone 
                                                                         target:self 
                                                                         action:@selector(releasePicker)];
[pickerBar setItems:@[flexSpace, doneBtn] animated:YES];    
self.textField.inputAccessoryView = pickerBar;

这样,工具栏将“附加”到您inputView的身上,并在激活文本字段时自动出现,并在您的选择器激活时消失!

编辑/更新

如果您有兴趣使用以 IB 为重点的方法,我确实找到了可以帮助您的链接: https ://stackoverflow.com/a/10705161/1429262

于 2013-08-07T17:01:16.663 回答
0

更简单的是在 UITextField 上使用 inputAccessoryView 属性。您的 inputView 是键盘还是选择器都无关紧要。将您的工具栏设置为 inputAccessoryView,它将使用完成按钮在您的选择器顶部进行动画处理,然后在 resignFirstResponder 上进行动画处理。

于 2013-08-07T16:36:37.823 回答