Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
386 views
in Technique[技术] by (71.8m points)

iphone - How to add UIView on the top of all views?

I have such code:

@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];
}

Then I'm use it so:

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

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The recommended solutions which handle device orientation properly are

  • 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()

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...