OStack程序员社区-中国程序员成长平台

标题: iOS 11 导航栏/uibarbutton 项目渲染问题 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 13:46
标题: iOS 11 导航栏/uibarbutton 项目渲染问题

我正在尝试向左侧添加多个 UIBarButtonItem 项目,并在右侧添加一个。这在 iOS10 上运行良好,但 iOS11 似乎已经破坏了它。

这都是用 Xamarin 编写的,但是类/方法名称等几乎都映射起来了,所以随心所欲地回答。

当使用最简单的方法添加两个按钮时,直接在按钮上设置图像/ Action ,我只得到两个中的一个,而且它很大,居中,我的标题丢失了:

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


以前我们创建 UIImage 项并将它们添加为自定义 View (请忽略可怕的做法,这是继承的):

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;

我得到一个奇怪的、伸出的“后退”按钮和一个非常小的汉堡菜单: with custom views, stretched back


最后,虽然我不同意,但我们的客户坚持使用自定义后退按钮。当我采用我认为正确的方法时,使用 native 后退按钮,它有点工作,我的标题 View 又回来了。这将作为权宜之计,但这并不是我们真正想要的用户体验:

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

谢谢!



Best Answer-推荐答案


请参阅 UIBarButtonItem on iOS 11

UIBarButtonItem 使用自动布局而不是处理框架。

这就是按钮被拉伸(stretch)的原因。

修改:

widthConstraint = leftBackBtn.WidthAnchor.ConstraintEqualTo(27);
heightConstraint = leftBackBtn.HeightAnchor.ConstraintEqualTo(20);
widthConstraint.Active = true;
heightConstraint.Active = true;

关于iOS 11 导航栏/uibarbutton 项目渲染问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46408217/






欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) Powered by Discuz! X3.4