【发布时间】:2013-11-24 20:08:07
【问题描述】:
我正在使用Apple KeyBoardAccessory Documentation 在我的键盘上方添加一个 UIView。问题是 UIView 根本没有显示。下面是我的代码:
头文件:
@property (nonatomic, weak) IBOutlet UITextView *textView;
@property (nonatomic, weak) IBOutlet UIView *accessoryView;
@property (nonatomic, weak) IBOutlet UIBarButtonItem *editButton;
@property (nonatomic, weak) IBOutlet UIBarButtonItem *doneButton;
寻找键盘通知:
- (void)viewDidLoad {
[super viewDidLoad];
[self.textView becomeFirstResponder];
// observe keyboard hide and show notifications to resize the text view appropriately
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillHide:)
name:UIKeyboardWillHideNotification
object:nil];
// set the right bar button item initially to "Edit" state
self.navigationItem.rightBarButtonItem = self.editButton;
}
- (BOOL)textViewShouldBeginEditing:(UITextView *)aTextView {
// you can create the accessory view programmatically (in code), or from the storyboard
if (self.textView.inputAccessoryView == nil) {
self.textView.inputAccessoryView = self.accessoryView;
}
self.navigationItem.rightBarButtonItem = self.doneButton;
return YES;
}
终于想搬了textField:
#
pragma mark - Responding to keyboard events
- (void)keyboardWillShow:(NSNotification *)notification {
/*
Reduce the size of the text view so that it's not obscured by the keyboard.
Animate the resize so that it's in sync with the appearance of the keyboard.
*/
NSDictionary *userInfo = [notification userInfo];
// Get the origin of the keyboard when it's displayed.
NSValue *aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
// Get the top of the keyboard as the y coordinate of its origin in self's view's
// coordinate system. The bottom of the text view's frame should align with the top
// of the keyboard's final position.
//
CGRect keyboardRect = [aValue CGRectValue];
keyboardRect = [self.view convertRect:keyboardRect fromView:nil];
CGFloat keyboardTop = keyboardRect.origin.y;
CGRect newTextViewFrame = self.view.bounds;
newTextViewFrame.size.height = keyboardTop - self.view.bounds.origin.y;
// Get the duration of the animation.
NSValue *animationDurationValue = [userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];
NSTimeInterval animationDuration;
[animationDurationValue getValue:&animationDuration];
// Animate the resize of the text view's frame in sync with the keyboard's appearance.
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:animationDuration];
self.textView.frame = newTextViewFrame;
[UIView commitAnimations];
}
【问题讨论】:
-
您确认
self.accessoryView不是nil? -
我该如何解决这个问题? @rmaddy
-
使用调试器或添加日志语句。
-
我仍然一无所获@rmaddy 我使用教程中的相同代码,所以我不知道问题是什么。
-
你有没有给
accessoryView出口分配任何东西?
标签: ios objective-c uiview uitextview