How can I change my AccessoryView input parameter when the device is rotated?

I am attaching a UIToolbar to my UITextView as an inputAccessoryView to add a button to close the keyboard. This works great and looks correct when the device is in portrait mode. But it’s hard for me to understand how to resize the toolbar to a lower height used for toolbars when the device is in landscape mode.

I am adding a toolbar to my text delegate -textViewShouldBeginEditing: ::

 if (!textView.inputAccessoryView) { UIToolbar *keyboardBar = [[UIToolbar alloc] init]; keyboardBar.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleTopMargin; keyboardBar.barStyle = UIBarStyleBlackTranslucent; UIBarButtonItem *spaceItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil]; UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(dismissKeyboard:)]; [keyboardBar setItems:[NSArray arrayWithObjects:spaceItem, doneButton, nil]]; [spaceItem release]; [doneButton release]; [keyboardBar sizeToFit]; textView.inputAccessoryView = keyboardBar; [keyboardBar release]; } 

I get strange behavior from this code in landscape mode. If I start editing in landscape mode, the terrain height will be on the toolbar, but the Finish button will be drawn halfway on the screen. If I then return to portrait mode, the Finish button will appear in the right place and stay in the right place when I return to landscape mode.

If I start editing in portrait mode, the toolbar is drawn from the portrait height, but the "Finish" button is displayed in the right place. If I then rotate to landscape mode, the toolbar remains upright, but the Finish button is still in the correct position.

Any suggestions for resizing this device while rotating the device? I really hope that there is a more automatic way than manually connecting the magic height numbers in one of the rotation events of the view controller.

+7
source share
2 answers

This is a difficult problem. I solved this in the past by adjusting the frame when the accessory opens after turning. Try something like this:

 @interface RotatingTextInputToolbar : UIToolbar @end @implementation RotatingTextInputToolbar - (void) layoutSubviews { [super layoutSubviews]; CGRect origFrame = self.frame; [self sizeToFit]; CGRect newFrame = self.frame; newFrame.origin.y += origFrame.size.height - newFrame.size.height; self.frame = newFrame; } @end 

And using RotatingTextInputToolbar instead of UIToolbar in your code above.

+2
source

Voila:

 @interface AccessoryToolbar : UIToolbar @end @implementation AccessoryToolbar -(id)init { if (self = [super init]) { [self updateSize]; NSNotificationCenter* nc = [NSNotificationCenter defaultCenter]; [nc addObserver:self selector:@selector(orientationDidChange:) name:UIApplicationDidChangeStatusBarOrientationNotification object:NULL]; } return self; } -(void)dealloc { [[NSNotificationCenter defaultCenter] removeObserver:self]; [super dealloc]; } -(void)orientationDidChange:(NSNotification*)notification { [self updateSize]; } -(void)updateSize { bool landscape = UIInterfaceOrientationIsLandscape([[UIApplication sharedApplication] statusBarOrientation]); CGSize size = UIScreen.mainScreen.bounds.size; if (landscape != size.width > size.height) std::swap(size.width, size.height); if (size.height <= 320) size.height = 32; else size.height = 44; self.frame = CGRectMake(0, 0, size.width, size.height); } @end 
+1
source

All Articles