iPhone - 关闭键盘以便UIToolbar保持不变

时间:2013-12-05 05:53:57

标签: ios iphone uitoolbar

我正在开发一款iPhone应用,只要键盘出现,我就必须添加 UIToolbar When textview is clicked

现在,当我关闭键盘时,[textView resignFirstResponder]整个UIToolbar从视图中被解除。我想要实现的是将键盘关闭到某个Y点,以便UIToolbar保持在底部。 when keyboard is dismissed

非常感谢任何帮助。

感谢。

编辑 UIToolbar代码

UIButton *firstButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 81, 45)];
[firstButton setImage:[UIImage imageNamed:@"askQuestionCloseButton.png"] forState:UIControlStateNormal];
[firstButton addTarget:self action:@selector(firstButtonPressed) forControlEvents:UIControlEventTouchUpInside];

UIButton *secondButton = [[UIButton alloc] initWithFrame:CGRectMake(79, 0, 80, 45)];
[secondButton setImage:[UIImage imageNamed:@"askQuestionUploadPic.png"] forState:UIControlStateNormal];
[secondButton addTarget:self action:@selector(secondButtonPressed) forControlEvents:UIControlEventTouchUpInside];

UIButton *thirdButton = [[UIButton alloc] initWithFrame:CGRectMake(158, 0, 80, 45)];
[thirdButton setImage:[UIImage imageNamed:@"askQuestionSetLocation.png"] forState:UIControlStateNormal];
[thirdButton addTarget:self action:@selector(firstButtonPressed) forControlEvents:UIControlEventTouchUpInside];


UIButton *fourthButton = [[UIButton alloc] initWithFrame:CGRectMake(238, 0, 80, 45)];
[fourthButton setImage:[UIImage imageNamed:@"askQuestionPost.png"] forState:UIControlStateNormal];
[fourthButton addTarget:self action:@selector(firstButtonPressed) forControlEvents:UIControlEventTouchUpInside];


UIView *leftBarItemsView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 45)];
[leftBarItemsView addSubview:firstButton];
[leftBarItemsView addSubview:secondButton];
[leftBarItemsView addSubview:thirdButton];
[leftBarItemsView addSubview:fourthButton];

//spacer
UIBarButtonItem *spacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
spacer.width = -12;

UIBarButtonItem *leftBarItem = [[UIBarButtonItem alloc] initWithCustomView:leftBarItemsView ];


UIToolbar *tipToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 47)];
tipToolbar.barStyle = UIBarButtonItemStyleBordered;

tipToolbar.items = [NSArray arrayWithObjects:spacer, leftBarItem, spacer, nil];
self.askQuestionTextField.inputAccessoryView = tipToolbar;

1 个答案:

答案 0 :(得分:0)

您可以使用UIKeyboardDidShowNotificationUIKeyboardDidHideNotification通知来跟踪键盘的可见性,当键盘可见时,您可以更改工具栏的框架。

示例:

[[NSNotificationCenter defaultCenter] addObserver:self 
                                         selector:@selector(keyboardDidShow:)
                                             name:UIKeyboardDidShowNotification
                                           object:nil];

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(keyboardDidHide:)
                                             name:UIKeyboardDidHideNotification
                                           object:nil];

- (void)keyboardDidShow: (NSNotification *) notif{
     // Change the frame of your toolbar
}

- (void)keyboardDidHide: (NSNotification *) notif{
     // Change the frame of your toolbar
}