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?
If you need a quick way to get hold of a view inside a complicated view hierarchy, you're looking for viewWithTag() – give it the tag to find and a view to search from, and this method will search all subviews, and all sub-subviews, and so on, until it finds a view with the matching tag number.
The superview is the immediate ancestor of the current view. The value of this property is nil when the view is not installed in a view hierarchy. To set the value of this property, use the addSubview(_:) method to embed the current view inside another view.
Views are for display, model objects are for data, controllers are the glue in between them.
The recommended solutions which handle device orientation properly are
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()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With