gpt4 book ai didi

iPhone - 出现键盘时工具栏向上移动

转载 作者:行者123 更新时间:2023-12-01 19:17:53 25 4
gpt4 key购买 nike

我在 NavigationController 中添加了一个带有 TextField 的工具栏

- (void)viewDidLoad
{
// ...

[self.navigationController setToolbarHidden:NO animated:YES];

UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(0, 2, 240, 36)];
textField.placeholder = @"Leave your comment here...";
textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;

UIBarButtonItem *commentTextFieldBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:textField];

self.toolbarItems = [NSArray arrayWithObject:commentTextFieldBarButtonItem];
}

当用户单击 TextField 时,我希望工具栏随键盘向上移动。
我试过 this method .工具栏向上移动,但变成了空的。难道我做错了什么?请帮帮我,谢谢!!

Screenshot 1 - Before

Screenshot 2 - After

最佳答案

最佳解决方案是设置 inputAccessoryViewUITextField像这样 :

#pragma mark - UITextFieldDelegate

-(void)textFieldDidBeginEditing:(UITextField *)textField{
// Call the createInputAccessoryView method we created earlier.
// By doing that we will prepare the inputAccView.

UIView *inputAccView = [self createInputAccessoryView];
// Now add the view as an input accessory view to the selected textfield.
[textField setInputAccessoryView:inputAccView];
}
-(UIView *)createInputAccessoryView{
// Create the view that will play the part of the input accessory view.
// Note that the frame width (third value in the CGRectMake method)
// should change accordingly in landscape orientation. But we don’t care
// about that now.
UIView *inputAccView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 40.0)];
// Set the view’s background color. We’ ll set it here to gray. Use any color you want.
[inputAccView setBackgroundColor:[[UIColor blackColor] colorWithAlphaComponent:0.20f]];
// We can play a little with transparency as well using the Alpha property. Normally
// you can leave it unchanged.
[inputAccView setAlpha: 0.8];

UIButton *btnDone = [UIButton buttonWithType:UIButtonTypeCustom];
[btnDone setFrame:CGRectMake(240.0, 6.0f, 70.0f, 28.0f)];
[btnDone setTitle:@"Done" forState:UIControlStateNormal];
[btnDone setBackgroundColor:[UIColor blackColor]];
[btnDone.layer setCornerRadius:6.0f];
[btnDone setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[btnDone addTarget:self action:@selector(btnDoneForKeyboardAction:) forControlEvents:UIControlEventTouchUpInside];
// Now that our buttons are ready we just have to add them to our view.
[inputAccView addSubview:btnDone];

return inputAccView;
}

另一种解决方案:
当 View Controller 出现时,我们希望收到键盘事件的通知,以便我们可以显示工具栏以响应键盘的出现和消失:
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}

- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];

[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}
为了响应键盘的出现,我们可以从屏幕底部向上移动工具栏:
- (void)keyboardWillShow:(NSNotification *)notification {
//set your toolbar frame here for upper side
}
当键盘关闭时,我们做相反的事情:
- (void)keyboardWillHide:(NSNotification *)notification {
//set your toolbar frame here for down side
}

关于iPhone - 出现键盘时工具栏向上移动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12002796/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com