如何在所有视图的顶部添加UIView? [英] How to add UIView on the top of all views?

查看:121
本文介绍了如何在所有视图的顶部添加UIView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的代码:

@interface Alert : UIView 
{

}
/*
- (void) initWithTitle:(NSString*)title message:(NSString*)message cancelButtonTitle:(NSString*)cancelButtonTitle otherButtonTitles:(NSString*)buttonTitle,... delegate:(id)delegate;
*/
@end


@implementation Alert

- (void) drawRect:(CGRect)rect 
{
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextClearRect(context, rect); // Очистим context

    CGContextSetRGBFillColor(context, 255, 0, 0, 1);
    CGContextFillRect(context, CGRectMake(20, 20, 100, 100));
}

- (void) show
{
    self.center = [[[UIApplication sharedApplication] keyWindow] center];
    [[[UIApplication sharedApplication] keyWindow] addSubview:self];
    [[[UIApplication sharedApplication] keyWindow] bringSubviewToFront:self];
}

然后我就这样使用它:

- (void)viewDidLoad
{
    [super viewDidLoad];
     Alert * alert = [[Alert alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
    [alert show];
    [alert release];
}

但它不起作用(我希望看到Alert高于所有其他视图)。你能帮助我吗?

But it doesn't work(I want to see Alert above all other views). Could you help me?

推荐答案

正确处理设备方向的推荐解决方案

  • pod AGWindowView It will automatically deal with any rotation and framechanges so you won't have to worry about that.
  • read and follow the official post https://developer.apple.com/library/content/qa/qa1688/_index.html
  • Add your view to the first subview, instead of adding it to the window

UIWindow* window = [UIApplication sharedApplication].keyWindow;
if (!window) 
    window = [[UIApplication sharedApplication].windows objectAtIndex:0];
[[[window subviews] objectAtIndex:0] addSubview:myView];


您也可以将其添加到窗口中。 但它不会处理设备方向。

You can also add it to the window. It will not handle device orientation, though.

let window = UIApplication.sharedApplication().keyWindow!
let view = UIView(frame: CGRect(x: window.frame.origin.x, y: window.frame.origin.y, width: window.frame.width, height: window.frame.height))
window.addSubview(v);
view.backgroundColor = UIColor.blackColor()

这篇关于如何在所有视图的顶部添加UIView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆