【问题标题】:Navigationbar barbuttonitem wrong frame in IOS 11 and xcode 9IOS 11和xcode 9中的导航栏barbuttonitem错误框架
【发布时间】:2017-11-22 06:26:39
【问题描述】:

我有一个 Viewcontroller,我在其中设置了三个社交媒体右导航栏按钮。以前我在 Xcode 8 中运行应用程序。但是现在当我将 Xcode 更新到 9.1 时,UI 受到了干扰。 Navbar 改变了它的宽度,现在分布在整个 Navbar 中。代码设置为宽度,但未按代码运行。我的代码是这样的,

UIImage* image1 = [UIImage imageNamed:@"fab.png"];
CGRect frameimg1 = CGRectMake(3,0,30,30);
UIButton *someButton1 = [[UIButton alloc] initWithFrame:frameimg1];
[someButton1 setBackgroundImage:image1 forState:UIControlStateNormal];
[someButton1 addTarget:self action:@selector(facebook)
     forControlEvents:UIControlEventTouchUpInside];
[someButton1 setShowsTouchWhenHighlighted:YES];

UIBarButtonItem *mailbutton1 =[[UIBarButtonItem alloc] initWithCustomView:someButton1];

UIImage* image2 = [UIImage imageNamed:@"tt.png"];
CGRect frameimg2 = CGRectMake(20,50,30,30);
UIButton *someButton2 = [[UIButton alloc] initWithFrame:frameimg2];
[someButton2 setBackgroundImage:image2 forState:UIControlStateNormal];
[someButton2 addTarget:self action:@selector(twitter)
     forControlEvents:UIControlEventTouchUpInside];
[someButton2 setShowsTouchWhenHighlighted:YES];

UIBarButtonItem *mailbutton2 =[[UIBarButtonItem alloc] initWithCustomView:someButton2];

    UIImage *image3 = [UIImage imageNamed:@"G+.png"];
CGRect frameimg3 = CGRectMake(0,30,30,30);
UIButton *someButton3 = [[UIButton alloc] initWithFrame:frameimg3];
[someButton3 setBackgroundImage:image3 forState:UIControlStateNormal];
[someButton3 addTarget:self action:@selector(gmail)
     forControlEvents:UIControlEventTouchUpInside];
[someButton3 setShowsTouchWhenHighlighted:YES];

UIBarButtonItem *mailbutton3 =[[UIBarButtonItem alloc] initWithCustomView:someButton3];


[self.navigationItem setRightBarButtonItems:[NSArray arrayWithObjects:mailbutton1,mailbutton2,mailbutton3, nil]];

现在,当我在 Xcode 9.1 中运行该应用程序时,它会显示我的导航栏按钮,如下所示,

【问题讨论】:

  • 为什么要为按钮设置框架?只需使用您的图像制作 UIBarButton。它会自动调整大小。
  • 我有三个按钮。 @Muneeba
  • 您正在使用 setRightBarButtonItems,它需要一个数组。因此,您可以将 UIBarButtons 添加到数组中并将其分配给 setRightBarButtonItems。您可以使用 UIBarButtonSystemItemFixedSpace 添加 UIbarbutton,并根据需要将其视为按钮之间的间隔。

标签: ios xcode uinavigationbar uibarbuttonitem ios11


【解决方案1】:

我认为ios 11 barbuttonitems 不适用于框架。你必须设置必要的约束。这里你的按钮宽度不合适,所以一旦尝试设置宽度,

[[someButton1.widthAnchor constraintEqualToConstant:30.0] setActive:YES];
[[someButton2.widthAnchor constraintEqualToConstant:30.0] setActive:YES];
[[someButton3.widthAnchor constraintEqualToConstant:30.0] setActive:YES];

为每个按钮设置宽度常量!

这样做后,如果您遇到身高问题,请为 heightAnchor 做同样的事情。

发生这种情况是因为您的图像必须大于按钮的大小,并且您没有对高度和宽度进行限制,因此您的按钮正在调整大小以调整图像大小!

【讨论】:

  • 我现在必须用我的代码设置这些约束吗? @狮子
  • 是的,只需在创建按钮后在代码中添加这一行。例如,您创建 someButton1 -> 设置框架 -> 设置所有属性,然后设置它的宽度约束,如:[[someButton1.widthAnchor constraintEqualToConstant:30.0] setActive:YES];!
  • 这对我造成了约束冲突。似乎已经应用了“_UITemporaryLayoutWidth”,我不知道如何删除。我尝试迭代按钮现有约束并删除宽度和高度,但它似乎不起作用。
  • 根据上面的评论:我至少摆脱了冲突,确保约束与按钮上的实际 UIImage 尺寸相同。该按钮也不再调整大小。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-07-10
  • 2018-03-25
  • 2018-03-04
  • 1970-01-01
  • 2017-11-10
  • 2018-02-12
  • 2018-03-12
相关资源
最近更新 更多