9

I have an app that does all UI programmatically without using auto layout or any sort of constraints. At the bottom of the screen is a UIToolbar. I am trying to lay this out to play well with the iPhone X screen's bottom safe area but due to the design of the VCs I cannot use layout constraints.

So in my layoutSubviews callback I am doing this:

if (@available(iOS 11.0, *)) {
    toolbarHeight += UIApplication.sharedApplication.keyWindow.safeAreaInsets.bottom;
}

Then later setting the UIToolbar's frame directly with toolBarHeight.

This works in that the toolbar is now the correct height and adjusts when you rotate to landscape, but the problem is that the Toolbar's buttons are not top aligned, but rather centered vertically. I can't figure out how to set the contentInsets or some way to top align the UIToolbar buttons without using constraints.

Any ideas?

Bilal
  • 18,478
  • 8
  • 57
  • 72
Dave Haupert
  • 1,036
  • 8
  • 7
  • Got the same problem! Did you find a solution in the end? – Clafou Dec 11 '17 at 22:29
  • This worked for me: https://stackoverflow.com/a/46684504/431492 . I had to convert to using constraints (still programatically) for just to toolbar. – awulf Dec 15 '17 at 00:16
  • 1
    @Clafou - I still haven't released but do have something working. What I did was actually a bit of a hack. I created a UIView subclass that includes a single subview- a UIToolbar. So I size the UIView to fit at the right area of the bottom but the toolbar has it's frame at the top of the UIView itself and uses sizeToFit to get its height. Thus the UIView adjusts to fill the entire space, but the Toolbar is the normal height and top aligned within that UIView. – Dave Haupert Dec 15 '17 at 16:58

1 Answers1

4

UIToolbar will automatically fill the safe area. Keep the toolbar height as you would have (eg 49pts), and position it so that the bottom of the toolbar is at the top of the bottom safe area guide.

self.toolbar.width = self.view.width
self.toolbar.height = 49.0
self.toolbar.bottom = self.view.height - self.view.safeAreaInsets.bottom

The above code is using UIView+FrameAdjustments.

Ric Santos
  • 15,419
  • 6
  • 50
  • 75