簡體   English   中英

UIview.hidden不會消失,而是隱藏在另一個視圖后面

[英]UIview.hidden doesn't disappear but hides behind another view

所以我試圖隱藏一個UIView但發生了一些奇怪的事情。 使用此行代碼后:

ntcCircleView.hidden = YES;

該視圖不會消失,但隱藏在另一個UIView的后面。

這是我使用的完整代碼:

UIView* NtcContainer=[[UIView alloc]initWithFrame:CGRectMake(self.view.frame.size.width - 40-17, 3, 40, 40)];
UIView* NtcView=[[UIView alloc]initWithFrame:CGRectMake(5, 0, 40, 40)];


notificationButton  = [ZFRippleButton buttonWithType:UIButtonTypeCustom];
notificationButton.frame = CGRectMake(0, 0, 40, 40);
notificationButton.layer.cornerRadius=menuButton.frame.size.width/2;
[notificationButton addTarget:self action:@selector(goToNotificationsList:) forControlEvents:UIControlEventTouchUpInside];
notificationImage=[[UIImageView alloc]initWithFrame:CGRectMake(10, 12, 20, 20)];
notificationImage.image=[UIImage imageNamed:[HotelStay sharedInstance].icon.Notification];

ntcCircleView = [[UIView alloc] initWithFrame:CGRectMake(20,5,16,16)];
ntcCircleView.alpha = 0.7;
ntcCircleView.layer.cornerRadius = ntcCircleView.frame.size.width/2;  // half the width/height
ntcCircleView.backgroundColor = [UIColor redColor];

ntcNumberLabel = [[UILabel alloc] initWithFrame:CGRectMake(0,0,16,16)];
ntcNumberLabel.textAlignment = NSTextAlignmentCenter;
[ntcNumberLabel setTextColor:[UIColor whiteColor]];
[ntcNumberLabel setFont:[UIFont fontWithName:@"HelveticaNeue" size:11.0]];

[ntcCircleView addSubview:ntcNumberLabel]; 
int ntcNum = [dataManager getUnreadNotificationNumber];
if (ntcNum==0)
{
    ntcCircleView.hidden = YES;
}else
{
    ntcNumberLabel.text = [NSString stringWithFormat:@"%i",ntcNum];
}

[NtcView addSubview:notificationImage];
[NtcView addSubview:notificationButton];
[NtcView addSubview:ntcCircleView];
[NtcContainer addSubview:NtcView];

[self.view addSubview:NtcContainer];

所以ntcCircleView隱藏之后,僅在notificationImage之后

使我發瘋的是,我成功地在另一個視圖中使用了完全相同的代碼,而最后一行只是不同之處。 而不是使用:

[self.view addSubview:NtcContainer];

我將視圖添加到導航欄中,如下所示:

UIBarButtonItem *ntcBarItem = [[UIBarButtonItem alloc] initWithCustomView:NtcContainer];
self.navigationItem.rightBarButtonItem = ntcBarItem;

我在這里想念什么?

UPDATE

我還注意到只有在使用時才會發生此錯誤

[self.navigationController popViewControllerAnimated:YES];

導航回到視圖。

您需要更新以下代碼段:

[NtcView addSubview:notificationImage];
[NtcView addSubview:notificationButton];
[NtcView addSubview:ntcCircleView];
[NtcContainer addSubview:NtcView];

至:

[NtcView addSubview:notificationButton];
[NtcView addSubview:ntcCircleView];
[NtcView addSubview:notificationImage];
[NtcContainer addSubview:NtcView];

它將解決您的問題。

我想到了。 我正在使用不推薦使用的方法(viewDidUnload)釋放通知觀察器。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM