Resize toolbar on rotation in iOS8

I am trying to get the toolbar at the bottom of my app to resize when I rotate the iphone / ipod. Navigation controller automatically resizes navbar in iOS8. It gets the correct height if it is already in the terrain when the action begins.

Same size as portrait after turning to landscape Now

Less because the activity was launched in landscape orientation Want

The toolbar has been added to the storyboard.

@IBOutlet var toolbar: UIToolbar!

      

I tried to change the toolbar rectangle to willRotateToInterfaceOrientation(toInterfaceOrientation: UIInterfaceOrientation, duration: NSTimeInterval)

Like this:

override func willRotateToInterfaceOrientation(toInterfaceOrientation: UIInterfaceOrientation, duration: NSTimeInterval) {
    var customToolbarFrame: CGRect!
    if toInterfaceOrientation == UIInterfaceOrientation.Portrait {
        customToolbarFrame = CGRectMake(0,self.view.bounds.size.height - 44, self.toolbar.frame.size.width, 44)
    }
    else {
        customToolbarFrame = CGRectMake(0,self.view.bounds.size.height - 32, self.toolbar.frame.size.width, 32)
    }
    UIView.animateWithDuration(duration, animations: {
        self.toolbar.frame = customToolbarFrame
    })
}

      

I also tried no animation but no luck.

However, it works if I do the same in didRotateFromInterfaceOrientation(fromInterfaceOrientation: UIInterfaceOrientation)

, but without animation it looks terrible because there is some delay.

Decision

from gabbler

  • I chose w Any h Compact

    for all iphones (I don't want it smaller on ipads)
  • My toolbar is selected
  • Set the height to 32px for the constraints and Update frames to New constraint elements
+3


source to share


3 answers


If you are using automatic layout, you can set different height restrictions in portrait and landscape, for example, add 32 height restrictions for the toolbar in wCompact | hCompact, you should be able to see a 32-level toolbar in landscape mode, here is a Toolbar Example



+4


source


I've done a lot of research and haven't found any documentation explaining how to change / fix the navigation bar height of a navigation controller in IOS8, but I'm creating a workaround using Autolayout.

Everyone first tried the same approach as you, but using the ViewWillTransitionToSize " function , because the methods for rotating the interface are deprecated.

iOS 8 Rotation Methods Deprecation - Backward Compatibility

-(void) viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator {

    [super viewWillTransitionToSize: size withTransitionCoordinator: coordinator];

    [coordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> context) {

        UIDeviceOrientation deviceOrientation   = [[UIDevice currentDevice] orientation];

        if (UIDeviceOrientationIsLandscape(deviceOrientation)) {
            NSLog(@"Will change to Landscape");

        }
        else {
            NSLog(@"Will change to Portrait");
        }


    }
    completion:^(id<UIViewControllerTransitionCoordinatorContext> context) {
        NSLog(@"finish Transition");
    }];


}

      

Second . I tried to create my own class inherited from UIToolbar and implement changes in rect method by initWithFrame method.

if you want to use a workaround to fix this issue using Auto Layout , follow these steps:

1- Drag the Toolbar to the Controller View and Pin Leading, Bottom and Height:



enter image description here

2- Controlling Drag and Drop from Toolbar to Container View and Output "Equal Widths"

enter image description here

3- Done:

Portrait

enter image description here

+1


source


I'm not sure if it is possible to create a storyboard toolbar that uses adaptation (because there is no container for the top limit of the toolbar).

You can tweak the height constraint and tweak it in code, however the navigation controller toolbar will already adjust your height (based on orientation) for you.

If you don't mind customizing your toolbar items in code (using the display control property toolbarItems

), you get free responsive customization.

self.toolbarItems = @[...];

      

Here's an example of how I customize the toolbar:

/**
 Setup a segmented control of bible versions, and add it to the toolbar
 */
- (void)setupBibleVersionToolbarItems
{
    self.versionSegmentedControl = [[UISegmentedControl alloc] initWithItems:@[@"ESV",
                                                                               @"KJV",
                                                                               @"NASB",
                                                                               @"NIV",
                                                                               @"NKJV",
                                                                               @"NLT"]];
    self.versionSegmentedControl.opaque = NO;
    [self.versionSegmentedControl addTarget:self
                                     action:@selector(versionChanged:)
                           forControlEvents:UIControlEventValueChanged];

    UIBarButtonItem *bibleVersionItem = [[UIBarButtonItem alloc]
                                         initWithCustomView:(UIView *)self.versionSegmentedControl];
    bibleVersionItem.width = 300.0f;
    UIBarButtonItem *flexibleSpace = [[UIBarButtonItem alloc]
                                      initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
                                      target:nil
                                      action:nil];

    self.toolbarItems = @[flexibleSpace, bibleVersionItem, flexibleSpace];
}

      

You can show or hide the navigation controller toolbar if needed by setting its property toolbarHidden

.

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.toolbarHidden = NO;
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    self.navigationController.toolbarHidden = YES;
}

      

0


source







All Articles