我有一个自定义视图,作为一个工具栏的故事板。该视图有三个按钮,我为ViewDidLoad中的每个按钮定义了自定义视图。ios查看导航到其他屏幕后消失
当我导航到不同的视图时,然后向后移动到这个原始屏幕,这些按钮上的文本和图像都丢失了。只有一个空的黑色条。这发生在一些设备上,但视图显示得很好,并且永远不会在其他设备上消失。
我已经试过:
我明确设置这些按钮的隐藏属性设置为false在viewWillAppear中
btn1.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);
}
}
按钮会出现,并正确绘制的第一次。然而,在我离开这个页面并回到它之后,底部只有一个黑色的视图。没有按钮。