gpt4 book ai didi

iOS 11 : UIBarButtonItem inside UIToolbar goes out of its place

转载 作者:行者123 更新时间:2023-11-29 00:10:49 24 4
gpt4 key购买 nike

我的屏幕应该是这样的。它的底部有一个 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);
}

感谢任何帮助。谢谢。

最佳答案

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

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

希望这能解决您的问题。

关于iOS 11 : UIBarButtonItem inside UIToolbar goes out of its place,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46514903/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com