我可以有一些 UIView 始终显示在iOS的顶部吗?

我的项目中有很多addSubview,但我需要有一个始终出现的小视图。所以有没有其他选择

[self.view bringSubViewToFront:myView];

谢谢

最佳答案

另一种选择(特别是如果您想重叠多个屏幕,例如带有徽标)-单独的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;

07-28 03:01