设置动画始终出现

时间:2013-08-07 16:17:39

标签: iphone ios objective-c animation uitoolbar

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

[pickerBar removeFromSuperview];

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

[self.view addSubview:pickerBar];

因此,触摸文本视图时会激活它。

问题是,当我再次点击文本字段时,toolBar会丢失其导航。

以下是大部分代码

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




        }
}];

}

2 个答案:

答案 0 :(得分:2)

inputViewinputAccessoryView之间存在差异。这就是你想要的:

// 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

答案 1 :(得分:0)

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