iOS 11:UIToolbar中的UIBarButtonItem不合适

时间:2017-10-01 17:21:11

标签: ios uibarbuttonitem uitoolbar

这就是我的屏幕应该是这样的。它底部有一个UIToolBar,它包含一个UIBarButtonItem。它在iOS 11之前运行良好。

enter image description here

但是在iOS 11中,UIBarButtonItem显示出它的位置。它显示在状态栏中。在模拟器中它工作正常,它显示在工具栏内。但是当我在运行iOS 11的iPhone 6s上运行它时,它显示在下面的屏幕中。

enter image description here

这是代码..

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);
}

感谢任何帮助。谢谢。

1 个答案:

答案 0 :(得分:1)

由于iOS Auto Layout已经引入UINavigationBar和UIToolbar元素。因此,解决问题的最佳方法是添加适当的约束。

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

希望这能解决你的问题。