iphone - 设置动画总是出现

标签 iphone ios objective-c animation uitoolbar

我制作了一个文本字段,其第一响应者是 datePicker。我还将一个工具栏附加到一个 View ,所以当点击文本字段时,工具栏会随着动画向上滑动。然后在工具栏上有一个完成按钮,可以让第一响应者辞职。我还希望它删除工具栏。为此,我添加了这个

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




        }
}];

}

最佳答案

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

关于iphone - 设置动画总是出现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18108497/

相关文章:

来自服务器的 iPhone 实时通知,无需使用 Apple 推送通知

ios - 在 Xamarin.iOS 中设置流量样式

ios - 如何在模拟器中将 iOS 应用程序启动到后台?

iphone - UIColor 清除颜色

iphone - iTunes 连接时区困惑

ios - 具有不同 Storyboard样式的多个 iOS 目标

ios - 使用来自 Parse.com PFQueryTableViewController 的 editActionsForRowAtIndexPath 提取行数据

objective-c - 为什么不用 "with"启动便利构造函数?

ios - 从 SDK 返回 App

ios - 从内存中删除未使用的 ViewController