[英]iOS toolbar above keyboard position not correct
我的應用程序大多數頁面為橫向,只有少數頁面為縱向。 該圖顯示了我的問題。 對於縱向模式,工具欄在鍵盤/日期選擇器上方的位置不正確。 我將設備方向設置為縱向/橫向向左/橫向向右。 在縱向頁面中,我的代碼
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
if ([NSObject isiPhone]) {
return UIInterfaceOrientationPortrait;
}
return UIInterfaceOrientationLandscapeLeft | UIInterfaceOrientationLandscapeRight;
}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
if ([NSObject isiPhone]) {
return UIInterfaceOrientationMaskPortrait;
}
return UIInterfaceOrientationMaskLandscape;
}
- (BOOL)shouldAutorotate{
return YES;
}
以下是我的工具欄代碼:
UIToolbar *toolbar=[[UIToolbar alloc]init];
toolbar.barTintColor=[UIColor lightTextColor];
toolbar.frame=CGRectMake(0, 0, 320, 44);
UIBarButtonItem *item2=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *item3=[[UIBarButtonItem alloc]initWithTitle:@"OK" style:UIBarButtonItemStylePlain target:self action:@selector(click)];
toolbar.items = @[item2,item3];
self.babyBirthdayTextfield.inputAccessoryView=toolbar;
該問題也出現在Web視圖上, 我的應用程序縱向視圖的結構
我已經解決了這個問題。 此情節提要的根控制器是一個模式窗口。 改變segue的種類。
您不必手動管理工具欄的框架。 只需使用textField的inputAccessoryView
,您就可以inputAccessoryView
進行以下操作:
UIToolbar* toolbar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 50)];
toolbar.barStyle = UIBarStyleBlackTranslucent;
toolbar.items = [NSArray arrayWithObjects:
[[UIBarButtonItem alloc]initWithTitle:@"Cancel" style:UIBarButtonItemStyleBordered target:self action:@selector(cancelButtonTapped)],
[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil],
[[UIBarButtonItem alloc]initWithTitle:@"Done" style:UIBarButtonItemStyleDone target:self action:@selector(doneButtonTapped)],
nil];
[toolbar sizeToFit];
self.textField.inputAccessoryView = toolbar;
希望這可以幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.