ios 如何在所有视图的顶部添加 UIView?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6622913/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to add UIView on the top of all views?
提问by Siarhei Fedartsou
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?
但它不起作用(我想在所有其他视图之上看到警报)。你可以帮帮我吗?
回答by Maciej
The recommended solutions which handle device orientation properly are
正确处理设备方向的推荐解决方案是
- pod AGWindowViewIt 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];
- pod AGWindowView它将自动处理任何旋转和框架变化,因此您不必担心。
- 阅读并关注官方帖子https://developer.apple.com/library/content/qa/qa1688/_index.html
将您的视图添加到第一个子视图,而不是将其添加到窗口
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()
回答by g212gs
There is also one other method
还有一种方法
Add the following method in the view will appear to add every time push/pop view controller
在view中添加如下方法会出现每次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];
}
回答by Nazmul Hasan
@MaciekWroc?aw answere swift version
@MaciekWroc?aw answere swift 版本
Swift2
斯威夫特2
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
迅捷3
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:
我正在为我的 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)!
}
}
回答by iOS
UIView *spinnerView;//This is your view
self.spinnerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, UIScreen.mainScreen.bounds.size.width, UIScreen.mainScreen.bounds.size.height)];
//Based on your requirement change width and height like self.view.bounds.size.width
self.spinnerView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.6];
// [self.view addSubview:self.spinnerView];
UIWindow *currentWindow = [UIApplication sharedApplication].keyWindow;
[currentWindow addSubview:self.spinnerView];
In Swift 5
在斯威夫特 5
let window = UIApplication.shared.keyWindow!
window.addSubview(self.spinnerView) // Add your view here
One more simple solution is :
一种更简单的解决方案是:
yourViewName.layer.zPosition = 1//Here fix your view name
回答by dayhacker
If you know what subview is at the top, use:
如果您知道顶部的子视图是什么,请使用:
UIView* topMostSubView = <your view's top most subview>
[self.view insertSubview:alert aboveSubview:topMostSubView];