问题
My app is iPad only and the root view is embedded in a navigation controller which has two sub views:
1) a UITextView
(not UITextField
) which covers the whole area except for the navigation bar.
2) another UIView
which serves as a tool bar. It covers the UITextView and initially stays at the bottom of root view.
Now I can make the "tool bar" goes up and down in sync with the virtual keyboard.
But there is one problem: if I rotates the device while the keyboard is showing, the "tool bar" no longer sticks to the top of virtual keyboard, instead it stays in the middle of the screen while rotating and falls down to meet the keyboard after rotation, which is quite ugly.
Currently I make the tool bar view goes up and down by dynamically adding and removing constraints on it and I am not sure whether this is a problem because I am only testing it using simulator.
Can anyone give me some advice on it?
Examples of such UITextView with tool bar at the bottom apps may be Document Pro or Pages.
回答1:
I will recommend using inputAccessoryView property of the UITextView
.
UIToolbar* toolbar = [[UIToolbar alloc] init];
toolbar.barStyle = UIBarStyleDefault;
toolbar.items = [NSArray arrayWithObjects:
[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCompose target:nil action:nil],
[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCamera target:nil action:nil],
[[UIBarButtonItem alloc]initWithTitle:@"Cancel" style:UIBarButtonItemStylePlain target:self action:nil],
[[UIBarButtonItem alloc]initWithTitle:@"Done" style:UIBarButtonItemStyleDone target:self action:nil],
nil];
[toolbar sizeToFit];
self.textfiled.inputAccessoryView = toolbar;
Simply with the above you will a toolbar sticking on top of the keyboard and it will have a nice and smooth animation during rotation. I haven't looked at the other app that you quotes, but I believe that's what they use in Pages.

回答2:
As an alternative, you can manually layout your toolbar without applying constraints. For that, you can:
Add method to handle keyboard appearing.
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWasShown:) name:UIKeyboardDidShowNotification object:nil];
In this method you need to calculate current keyboard height.
- (void)keyboardWasShown:(NSNotification*)aNotification
{
NSDictionary* info = [aNotification userInfo];
CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
self.keyboardHeight = MIN(kbSize.height, kbSize.width);
[self layoutElements];
}
This method also called when you rotate your device.
Then in your custom layoutElements
method you use pre-calculated keyboard height to position your toolbar.
You can use the size of your root view or screen size ([[UIScreen mainScreen] bounds].size
), but pay attention that in iOS7 and iOS8+ there are different values for width
and height
depending on orientation.
To ensure that your custom layout method (layoutElements
) is called you can also put it inside viewWillLayoutSubviews
method.
- (void) viewWillLayoutSubviews {
[super viewWillLayoutSubviews];
[self layoutElements];
}
来源:https://stackoverflow.com/questions/32550036/make-custom-view-stick-to-top-of-the-keyboard-while-rotating-device