Iphone 将动画设置为始终出现

Iphone 将动画设置为始终出现,iphone,ios,objective-c,animation,uitoolbar,Iphone,Ios,Objective C,Animation,Uitoolbar,我做了一个textfield谁的第一反应是一个日期选择器。我还为视图附加了一个工具栏,因此当点击textfield时,工具栏会随着动画向上滑动。然后在工具栏上有一个“完成”按钮,它会让第一响应者辞职。我还希望它删除工具栏。为此,我添加了这个 [pickerBar removeFromSuperview] 然后为了重新打开它,我做了这个 [self.view addSubview:pickerBar] 因此,当触摸textview时,它被激活 问题是,当我再次点击文本字段时,工具栏将失去导航功能

我做了一个textfield谁的第一反应是一个日期选择器。我还为视图附加了一个工具栏,因此当点击textfield时,工具栏会随着动画向上滑动。然后在工具栏上有一个“完成”按钮,它会让第一响应者辞职。我还希望它删除工具栏。为此,我添加了这个

[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];




        }
}];

}

更简单的方法是在UITextField上使用inputAccessoryView属性。inputView是键盘还是选择器无关紧要。将工具栏设置为inputAccessoryView,它将使用“完成”按钮在选择器顶部设置动画,然后在辞职FirstResponder时设置动画

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为重点的方法,我确实发现此链接可能会对您有所帮助:

我就是这么做的,我说的是我的工具栏是的,我知道你指的是你的工具栏。据我所知,你想要一个工具栏,上面有一个完成的项目,用你的选取器来设置动画,并显示在它的正上方。然后和你的拾荒者一起消失。上面,我已经描述了一个解决方案;使用UITextField的inputAccessoryView属性。另请参见UITextField文档和@Firo的示例。发生这种情况时,我必须移动工具栏,使其不可触摸。您必须移动它吗?我不明白你的意思。它出现在哪里?为什么你觉得你必须移动它?你把它搬到哪里去了?你期待什么?您说您已经尝试过这种方法,但该代码不存在问题。您是如何尝试设置inputAccessoryView的?(我知道我问了很多问题。与其在评论中回答,请随意编辑/更新你的问题)我已经尝试过了,但结果是我不得不移动工具栏才能看到我的整个日期选择器。然后它就休眠了。它掩盖了你的约会选择者的一部分?你需要提供更多的信息…知道你的和我的有什么不同,但现在看起来很棒。尽管唯一的方法是删除这行代码<代码>//设置inputView UIPickerView*pickerView=[[UIPickerView alloc]init];//其他配置//self.textField.inputView=pickerView@user2660874很高兴它起作用了,我刚刚添加了这些行,作为您需要设置选择器的提示。我以为你会用其他的机制来做。但很高兴一切顺利!