How to add UIView on the top of all views?
47,143
Solution 1
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()
Solution 2
There is also one other method
Add the following method in the view will appear to add every time push/pop view controller
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[[[UIApplication sharedApplication] keyWindow] addSubview:<yourViewObject>];
}
And for remove this view in next class add the following lines of code
-(void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
[<yourViewObject> removeFromSuperview];
}
Solution 3
@MaciekWrocław answere swift version
Swift2
var appDelegate = (UIApplication.sharedApplication().delegate! as! AppDelegate)
var backgrView = UIView(frame: CGRectMake(0, 0, UIScreen.mainScreen().bounds.size.width, UIScreen.mainScreen().bounds.size.height))
backgrView.backgroundColor = UIColor.redColor()
backgrView.alpha = 0.6
window?.addSubview(backgrView)
swift3
var appDelegate: AppDelegate? = (UIApplication.shared.delegate as? AppDelegate)
var backgrView = UIView(frame: CGRect(x: CGFloat(0), y: CGFloat(0), width: CGFloat(UIScreen.main.bounds.size.width), height: CGFloat(UIScreen.main.bounds.size.height)))
backgrView.backgroundColor = UIColor.red
backgrView.alpha = 0.6
window?.addSubview(backgrView)
and i am doing for my application about net connection in the AppDelegate:
func checkReachabilityUpdateUI() -> Int {
let remoteHostStatus = self.reachability?.currentReachabilityStatus()
if remoteHostStatus?.rawValue == 0 {
let backgrView = UIView(frame: CGRectMake(0, 60, UIScreen.mainScreen().bounds.size.width, 44))
let statusMessage = UILabel(frame: CGRect(x: 0, y: 0, width: UIScreen.mainScreen().bounds.size.width, height: 44))
statusMessage.text = "No internet Connection"
statusMessage.textAlignment = .Center
statusMessage.textColor = UIColor.whiteColor()
backgrView.backgroundColor = UIColor.redColor()
backgrView.addSubview(statusMessage)
backgrView.tag = 100
window?.addSubview(backgrView)
return (remoteHostStatus?.rawValue)!
}
else {
if let viewWithTag = self.window!.viewWithTag(100) {
print("Tag 100")
viewWithTag.removeFromSuperview()
}
else {
print("tag not found")
}
return (remoteHostStatus?.rawValue)!
}
}
Author by
Siarhei Fedartsou
Updated on November 18, 2021Comments
-
Siarhei Fedartsou over 2 years
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?