2

I'm trying to add multiple UIBarButtonItem items to the left, and one on the right. This worked fine up through iOS10, but iOS11 seems to have broken it.

This is all written with Xamarin, but the class/method names, etc., just about all map up, so answer however you want.

When using the simplest approach possible to add two buttons, setting an image/action directly on the button, I only get one of the two, and it's huge, and centered, and my title is lost:

var leftMenuBarBackBtn = new UIBarButtonItem(UIImage.FromBundle("arrowBackButton"), UIBarButtonItemStyle.Plain, leftBackButtonTap);
var leftMenuBarItem = new UIBarButtonItem(UIImage.FromBundle("menu"), UIBarButtonItemStyle.Plain, leftMenuButtonTap);
UIBarButtonItem[] navItems = { 
    leftMenuBarBackBtn,
    leftMenuBarItem
};

var rightMenuBarItem = new UIBarButtonItem(UIImage.FromBundle("bell").ImageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal), UIBarButtonItemStyle.Plain, rightMenuButtonTap);

NavigationItem.LeftItemsSupplementBackButton = false;
NavigationItem.LeftBarButtonItems = navItems;
NavigationItem.RightBarButtonItem = rightMenuBarItem;

Simple approach


Previously we were creating UIImage items and adding them as custom views (please ignore the horrible practices, this was inherited):

var leftBackBtn = new UIButton(UIButtonType.Custom);
leftBackBtn.SetImage(UIImage.FromBundle("arrowBackButton").ImageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal), UIControlState.Normal);
leftBackBtn.Frame = new CGRect(0, 0, 27, 20);
leftBackBtn.ContentMode = UIViewContentMode.ScaleAspectFit;
leftBackBtn.TouchUpInside += leftBackButtonTap;
UIBarButtonItem leftMenuBarBackBtn = new UIBarButtonItem(leftBackBtn);

var leftMenuBtn = new UIButton(UIButtonType.Custom);
leftMenuBtn.SetImage(UIImage.FromBundle("menu").ImageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal), UIControlState.Normal);
leftMenuBtn.Frame = new CGRect(0, 0, 27, 20);
leftMenuBtn.ContentMode = UIViewContentMode.ScaleAspectFit;
leftMenuBtn.TouchUpInside += leftMenuButtonTap;
UIBarButtonItem leftMenuBarItem = new UIBarButtonItem(leftMenuBtn);
UIBarButtonItem[] navItems = { leftMenuBarBackBtn, leftMenuBarItem };

var rightMenuBtn = new UIButton(UIButtonType.Custom);
rightMenuBtn.SetImage(UIImage.FromBundle("bell").ImageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal), UIControlState.Normal);
rightMenuBtn.Frame = new CGRect(0, 0, 19, 22);
rightMenuBtn.ContentMode = UIViewContentMode.ScaleAspectFit;
rightMenuBtn.TouchUpInside += rightMenuButtonTap;
UIBarButtonItem righttMenuBarItem = new UIBarButtonItem(rightMenuBtn);
UIBarButtonItem[] navItems2 = { righttMenuBarItem };

this.NavigationItem.LeftBarButtonItems = navItems;
this.NavigationItem.RightBarButtonItems = navItems2;

I get a weird, stretched out 'back' button, and an extremely tiny hamburger menu: with custom views, stretched back


Lastly, though I disagree with it, our client was insistent on a custom back button. When I went with what I would think is the proper approach, using the native back button, it somewhat works, and my title view is back. This'll work as a stop-gap but it's not really the UX we want:

var leftMenuBarItem = new UIBarButtonItem(UIImage.FromBundle("menu"), UIBarButtonItemStyle.Plain, leftMenuButtonTap);
var rightMenuBarItem = new UIBarButtonItem(UIImage.FromBundle("bell").ImageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal), UIBarButtonItemStyle.Plain, rightMenuButtonTap);

NavigationItem.LeftItemsSupplementBackButton = true;

NavigationItem.LeftBarButtonItem = leftMenuBarItem;
NavigationItem.RightBarButtonItem = rightMenuBarItem;

proper back button

Thanks!

cscott530
  • 1,658
  • 16
  • 25

1 Answers1

3

refer to UIBarButtonItem on iOS 11

UIBarButtonItem uses autolayout instead of dealing with frames.

That's why the button is got stretched .

Modify:

widthConstraint = leftBackBtn.WidthAnchor.ConstraintEqualTo(27);
heightConstraint = leftBackBtn.HeightAnchor.ConstraintEqualTo(20);
widthConstraint.Active = true;
heightConstraint.Active = true;
Community
  • 1
  • 1
ColeX
  • 14,062
  • 5
  • 43
  • 240
  • Works perfectly, thank you! Did not realize they changed that. – cscott530 Sep 29 '17 at 18:55
  • 1
    `heightConstraint` and `widthConstraint`...? WTF are they, lol? Actually, I worked it out, I'll edit the question to reflect sensibleness :) – Maverick Oct 11 '17 at 03:49