我有一個自定義視圖,作爲一個工具欄的故事板。該視圖有三個按鈕,我爲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);
}
}
按鈕會出現,並正確繪製的第一次。然而,在我離開這個頁面並回到它之後,底部只有一個黑色的視圖。沒有按鈕。