首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >iOS 11: UIBarButtonItem inside UIToolbar走出自己的位置

iOS 11: UIBarButtonItem inside UIToolbar走出自己的位置
EN

Stack Overflow用户
提问于 2017-10-02 01:21:11
回答 1查看 622关注 0票数 1

这就是我的屏幕应该是什么样子。它的底部有一个UIToolBar,并且包含一个UIBarButtonItem。在iOS 11之前,它工作得很好。

但在iOS 11中,UIBarButtonItem正在展示自己的位置。它将显示在状态栏中。在模拟器中,它工作正常,并且在工具栏中显示。但当我在运行iOS 11的iPhone 6s上运行它时,它的显示如下图所示。

这是代码..

代码语言:javascript
运行
复制
public override void ViewDidAppear(bool animated)
{
    base.ViewDidAppear(animated);
    UIBarButtonItem[] bArray = {
        getSetAsHomeButton(apiCall, this, 0),
        new UIBarButtonItem(UIBarButtonSystemItem.FlexibleSpace)
    };
    SetToolbarItems(bArray, true);
}

public static UIBarButtonItem getSetAsHomeButton(ICommonApiCall commonApiCall, UIViewController controller, int status)
{
    var view = new UIButton ();
    view.Layer.CornerRadius = 8;
    view.BackgroundColor = UIColor.White;

    view.TranslatesAutoresizingMaskIntoConstraints = false;
    view.WidthAnchor.ConstraintEqualTo(108).Active = true;
    view.HeightAnchor.ConstraintEqualTo(32).Active = true;

    var HomeIcon = new UILabel (new RectangleF (0, 0, 21, 21));
    HomeIcon.Font = FontAwesome.Font (22);
    HomeIcon.Text = FontAwesome.FAHome;
    HomeIcon.TextColor = PlatformConstants.PrimaryColor;

    var HomeLabel = new UILabel (new RectangleF (25, 0, 64, 44));
    HomeLabel.Text = "Set as default";
    HomeLabel.Font = UIFont.FromName (PlatformConstants.PrimaryFont + "-Medium", 11);
    HomeLabel.TextColor = PlatformConstants.PrimaryColor;
    HomeLabel.Lines = 0;
    HomeLabel.SizeToFit ();

    view.AddSubview (HomeIcon);
    view.AddSubview (HomeLabel);

    HomeIcon.TranslatesAutoresizingMaskIntoConstraints = false;
    HomeLabel.TranslatesAutoresizingMaskIntoConstraints = false;

    var hSpaceLeading = NSLayoutConstraint.Create (HomeIcon, NSLayoutAttribute.Leading,
                                   NSLayoutRelation.Equal, view, NSLayoutAttribute.Leading, 1, 6);
    view.AddConstraint (hSpaceLeading);

    var HomeIconCenterY = NSLayoutConstraint.Create (HomeIcon, NSLayoutAttribute.CenterY,
                               NSLayoutRelation.Equal, view, NSLayoutAttribute.CenterY, 1, 0);
    view.AddConstraint (HomeIconCenterY);

    var HomeLabelCenterY = NSLayoutConstraint.Create (HomeLabel, NSLayoutAttribute.CenterY,
                               NSLayoutRelation.Equal, view, NSLayoutAttribute.CenterY, 1, 0);
    view.AddConstraint (HomeLabelCenterY);

    var hSpace = NSLayoutConstraint.Create (HomeLabel, NSLayoutAttribute.Left,
                            NSLayoutRelation.Equal, HomeIcon, NSLayoutAttribute.Right, 1, 4);
    view.AddConstraint (hSpace);

    var HomeLabelWidth = NSLayoutConstraint.Create (HomeLabel, NSLayoutAttribute.Width,
                              NSLayoutRelation.Equal, null, NSLayoutAttribute.NoAttribute, 1, 80);
    view.AddConstraint (HomeLabelWidth);

    view.LayoutIfNeeded ();

    view.TouchUpInside += delegate {
        SetAsHome (commonApiCall, controller, status, view).ContinueWith (t => Console.WriteLine (t.Exception),
                                   TaskContinuationOptions.OnlyOnFaulted);
    };

    return new UIBarButtonItem (view);
}

任何帮助都是非常感谢的。谢谢。

EN

回答 1

Stack Overflow用户

发布于 2017-10-10 16:54:13

因为在UINavigationBar和UIToolbar元素中引入了iOS自动布局。因此,解决问题的最好方法是添加适当的约束。

在这里您可以找到有关该问题的更多帮助和详细信息:https://forums.developer.apple.com/thread/80075

希望这能解决你的问题。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46514903

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档