【发布时间】:2014-05-08 20:23:40
【问题描述】:
我可以有一些 UIView 在 iOS 中始终显示在顶部吗?
我的项目中有很多addSubview,但我需要一个始终出现的小视图。那么除了
[self.view bringSubViewToFront:myView];
谢谢
【问题讨论】:
标签: ios iphone objective-c cocoa-touch ios7
我可以有一些 UIView 在 iOS 中始终显示在顶部吗?
我的项目中有很多addSubview,但我需要一个始终出现的小视图。那么除了
[self.view bringSubViewToFront:myView];
谢谢
【问题讨论】:
标签: ios iphone objective-c cocoa-touch ios7
还有一个选项(特别是如果您想重叠多个屏幕,例如带有徽标)- 单独的 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;
【讨论】:
另一个选项是设置layer.zPosition 的UIView.
你需要添加
#import <QuartzCore/QuartzCore.h>
框架到您的.m file.
并设置这样的
myCustomView.layer.zPosition = 101;// set maximum value as per your requirement.
更多关于layer.zPosition read this documentation.的信息
讨论
此属性的默认值为 0。更改此属性的值会更改屏幕上图层的从前到后的顺序。这会影响框架矩形重叠的图层的可见性。
【讨论】:
另一种选择是在此始终位于顶部的子视图下方添加其他子视图。例如:
[self.view insertSubview:subview belowSubview:_topSubview];
如果您搜索此类,Interface Builder 没有解决方案。它应该以编程方式完成。如果您不想每次都使用bringSubviewToFront:,只需在此下方插入其他子视图即可。
【讨论】:
很多时候您的视图没有出现在 viewDidLoad 中,或者,如果您的视图来自 parentViewController(例如在模态 segue 等许多转换中),您只能在 viewDidAppear 中看到 parentViewController:
尝试将bringSubviewToFront 放入:
-(void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[self.view bringSubViewToFront:myView];
// or if your view is attached in parentViewController
[self.parentViewController.view bringSubViewToFront:myView];
}
祝你好运!
【讨论】: