2014-03-29 320 views
1

我可以有一些UIView它將永遠出現在iOS的頂端?iOS - UIView始終在前面沒有BringSubviewToFront

在我的項目中有很多addSubview,但我需要一個小視圖,它總是會出現。那麼,有沒有其他選擇,比

[self.view bringSubViewToFront:myView]; 

感謝

回答

2

還有一個選項(特別是如果你想重疊幾個屏幕,例如logo) - 單獨的UIWindow。使用windowLevel設置新窗口的級別。

UILabel *devLabel = [UILabel new]; 
devLabel.text = @" DEV "; 
devLabel.font = [UIFont systemFontOfSize:10]; 
devLabel.textColor = [UIColor grayColor]; 
[devLabel sizeToFit]; 

CGSize screenSize = [[UIScreen mainScreen] bounds].size; 
static UIWindow *notificationWindow; 
notificationWindow = [[UIWindow alloc] initWithFrame: 
           CGRectMake(screenSize.width - devLabel.width, screenSize.height - devLabel.height, 
           devLabel.width, devLabel.height)]; 
notificationWindow.backgroundColor = [UIColor clearColor]; 
notificationWindow.userInteractionEnabled = NO; 
notificationWindow.windowLevel = UIWindowLevelStatusBar; 
notificationWindow.rootViewController = [UIViewController new]; 
[notificationWindow.rootViewController.view addSubview:devLabel]; 
notificationWindow.hidden = NO; 
0

另一種選擇是增加低於這個總是在最頂部子視圖等子視圖。例如:

[self.view insertSubview:subview belowSubview:_topSubview]; 

如果您搜索此類型,則無法使用Interface Builder解決方案。它應該以編程方式完成。如果您不想每次都使用bringSubviewToFront:,只需在其中插入其他子視圖即可。

3

另一種選擇是設置你的UIView.

您需要

#import <QuartzCore/QuartzCore.h> 

框架添加到您的.m file.

layer.zPosition並且被設置成一樣

myCustomView.layer.zPosition = 101;// set maximum value as per your requirement. 

有關的更多信息layer.zPosition read this documentation.

討論
該屬性的缺省值是0改變這個屬性的值改變屏幕上的層的前到後的順序。這可能會影響幀矩形重疊的圖層的可見性。

0

很多時候,你的觀點並沒有出現在viewDidLoad中,或者,如果你的觀點來自parentViewController(例如像模式賽格瑞許多過渡..)你可以看到parentViewController只有在viewDidAppear這樣:

嘗試把把SubviewToFront放在:

-(void)viewDidAppear:(BOOL)animated { 
    [super viewDidAppear:animated]; 
    [self.view bringSubViewToFront:myView]; 
// or if your view is attached in parentViewController 
    [self.parentViewController.view bringSubViewToFront:myView]; 
} 

祝你好運!