Chris Chris - 15 days ago 7
iOS Question

make custom view stick to top of the keyboard while rotating device

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.

enter image description here

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.

enter image description here

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.

Answer

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.

Comments