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

标题: ios - IOS 11和xcode 9中的导航栏barbuttonitem错误框架 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 03:20
标题: ios - IOS 11和xcode 9中的导航栏barbuttonitem错误框架

我有一个 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 actionselector(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 actionselector(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 actionselector(gmail)
     forControlEvents:UIControlEventTouchUpInside];
[someButton3 setShowsTouchWhenHighlighted:YES];

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


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

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



Best Answer-推荐答案


我认为 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 执行相同操作。

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

关于ios - IOS 11和xcode 9中的导航栏barbuttonitem错误框架,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47428137/






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