Iphone 如何在所有视图的顶部添加UIView?
我有这样的代码:Iphone 如何在所有视图的顶部添加UIView?,iphone,objective-c,ios,Iphone,Objective C,Ios,我有这样的代码: @interface Alert : UIView { } /* - (void) initWithTitle:(NSString*)title message:(NSString*)message cancelButtonTitle:(NSString*)cancelButtonTitle otherButtonTitles:(NSString*)buttonTitle,... delegate:(id)delegate; */ @end @implementatio
@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];
}
但它不起作用(我希望看到警报高于所有其他视图)。您能帮助我吗?正确处理设备方向的推荐解决方案有
- 豆荚 它将自动处理任何旋转和帧更改,因此您不必担心这一点
- 阅读并遵循官方公告
- 将视图添加到第一个子视图,而不是将其添加到窗口
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* topMostSubView = <your view's top most subview>
[self.view insertSubview:alert aboveSubview:topMostSubView];
UIView*topMostSubView=还有一种方法
在视图中添加以下方法将在每次推/弹出视图控制器时显示为添加
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[[[UIApplication sharedApplication] keyWindow] addSubview:<yourViewObject>];
}
-(void)视图将显示:(BOOL)动画
{
[超级视图将显示:动画];
[[[UIApplication sharedApplication]键窗口]添加子视图:];
}
要在下一个类中删除此视图,请添加以下代码行
-(void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
[<yourViewObject> removeFromSuperview];
}
-(void)视图将消失:(BOOL)动画
{
[超级视图将消失:动画];
[removeFromSuperview];
}
@MaciekWrocław answere swift版本
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)
我正在为我的应用程序在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)!
}
}
斯威夫特5
let window = UIApplication.shared.keyWindow!
window.addSubview(self.spinnerView) // Add your view here
另一个简单的解决方案是:
yourViewName.layer.zPosition = 1//Here fix your view name
第一种解决方案不考虑设备旋转。第二种解决方案在出现模态viewcontroller时不起作用。第二种用于ipad的解决方案工作得很好,谢谢:)这是一个很大的假设。
let window = UIApplication.shared.keyWindow!
window.addSubview(self.spinnerView) // Add your view here
yourViewName.layer.zPosition = 1//Here fix your view name