我在故事板上有一个自定义视图,它充当工具栏。此视图有三个按钮,我为ViewDidLoad中的每个按钮定义自定义视图。
当我导航到其他视图,然后向后移动到此原始屏幕时,这些按钮上的文本和图像都丢失了。只有一个空的黑条。对于某些设备会发生这种情况,但视图显示得很好,并且永远不会在其他设备上消失。
我尝试过的事情:
我在ViewWillAppear
中明确将这些按钮的隐藏属性设置为falsebtn1.Hidden = false; btn2.Hidden = false; btn3.Hidden = false;
我还尝试在ViewWillAppear中定义按钮而不是ViewDidLoad,看看是否能解决问题。他们仍然消失。
以下是我的代码:
public override void ViewWillAppear(bool value)
{
try
{
base.ViewWillAppear(value);
//icon setup
//the space between the image and modelFilterText
var spacing = 0.3f;
var icon1 = UIImage.FromBundle("Icons/Home/icon1.png");
btn1.SetImage(icon1, UIControlState.Normal);
btn1.SetTitle("ONE", UIControlState.Normal);
btn1.SetTitleColor(UIColor.LightGray, UIControlState.Normal);
btn1.Font = UIFont.FromName("BankGothicBT-Light", 12f);
btnRequest.TouchUpInside += (object sender, EventArgs e) =>
{
NavigationItem.BackBarButtonItem = new UIBarButtonItem("", UIBarButtonItemStyle.Plain, null);
NavigationController.PushViewController(view1, true);
};
// lower the modelFilterText and push it left so it appears centered
// below the image
var imageSize = btn1.ImageView.Image.Size;
btn1.TitleEdgeInsets = new UIEdgeInsets(0.0f, -imageSize.Width, -(imageSize.Height + spacing),
0.0f);
// raise the image and push it right so it appears centered
// above the modelFilterText
var titleSize = btn1.TitleLabel.Text.StringSize(btn1.TitleLabel.Font);
btn1.ImageEdgeInsets = new UIEdgeInsets(-(titleSize.Height + spacing), 0.0f, 0.0f,
-titleSize.Width);
//Second Icon
var icon2 = UIImage.FromBundle("Icons/Home/icon2.png");
btn2.SetImage(icon2, UIControlState.Normal);
btn2.SetTitle("TWO", UIControlState.Normal);
btn2.SetTitleColor(UIColor.LightGray, UIControlState.Normal);
btn2.Font = UIFont.FromName("BankGothicBT-Light", 12f);
btn2.TouchUpInside +=
(object sender, EventArgs e) =>
{
NavigationItem.BackBarButtonItem = new UIBarButtonItem("", UIBarButtonItemStyle.Plain, null);
};
// lower the modelFilterText and push it left so it appears centered
// below the image
var imgSize = btn2.ImageView.Image.Size;
btn2.TitleEdgeInsets = new UIEdgeInsets(0.0f, -imgSize.Width, -(imgSize.Height + spacing), 0.0f);
// raise the image and push it right so it appears centered
// above the modelFilterText
var titleLength = btn2.TitleLabel.Text.StringSize(btn2.TitleLabel.Font);
btn2.ImageEdgeInsets = new UIEdgeInsets(-(titleLength.Height + spacing), 0.0f, 0.0f,
-titleLength.Width);
//Third Icon
var icon3 = UIImage.FromBundle("Icons/Home/icon3.png");
btn3.SetImage(icon3, UIControlState.Normal);
btn3.SetTitle("THREE", UIControlState.Normal);
btn3.SetTitleColor(UIColor.LightGray, UIControlState.Normal);
btn3.Font = UIFont.FromName("BankGothicBT-Light", 12f);
btn3.TouchUpInside += (object sender, EventArgs e) =>
{
NavigationItem.BackBarButtonItem = new UIBarButtonItem("", UIBarButtonItemStyle.Plain, null);
NavigationController.PushViewController(view3, true);
};
// lower the modelFilterText and push it left so it appears centered below the image
var imagSize3 = btn3.ImageView.Image.Size;
btn3.TitleEdgeInsets = new UIEdgeInsets(0.0f, -imagSize3.Width, -(imagSize3.Height + spacing),
0.0f);
// raise the image and push it right so it appears centered
// above the modelFilterText
var titleLength3 = btn3.TitleLabel.Text.StringSize(btn3.TitleLabel.Font);
btn3.ImageEdgeInsets = new UIEdgeInsets(-(titleLength3.Height + spacing), 0.0f, 0.0f,
-titleLength3.Width);
}
catch (Exception ex)
{
Console.WriteLine("View will appear error " + ex.Message + ex.StackTrace);
}
}
按钮出现并在第一次正确绘制。然而,在我离开这个页面然后回到它之后,底部只有一个黑色的视图。没有按钮。
答案 0 :(得分:1)
我了解到按钮实际上并没有消失。它们被iOS工具栏遮挡。
例如,如果我导航到具有iOS工具栏的屏幕,然后向后导航回此主页,则此后所有其他屏幕的NavigationController.SetToolbarHidden
属性设置为true。
所以我需要做的是在本主页的ViewWillAppear中将其设置为false,因此它不会遮挡我的自定义视图。
NavigationController.SetToolbarHidden(true,false)