【问题标题】:How to add UIView on the top of all views?如何在所有视图的顶部添加 UIView?
【发布时间】:2011-10-01 03:54:24
【问题描述】:

我有这样的代码:

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

但它不起作用(我希望看到 Alert 高于所有其他视图)。你能帮帮我吗?

【问题讨论】:

    标签: ios objective-c iphone cocoa-touch uiview


    【解决方案1】:
    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];
    

    在 Swift 5 中

    let window = UIApplication.shared.keyWindow! 
    window.addSubview(self.spinnerView) // Add your view here
    

    一个更简单的解决方案是:

    yourViewName.layer.zPosition = 1//Here fix your view name
    

    【讨论】:

      【解决方案2】:

      正确处理设备方向的推荐解决方案是

      • 吊舱 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];
        

      您也可以将其添加到窗口中。 但它不会处理设备方向。

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

      【讨论】:

      • 第一个解决方案不考虑设备旋转。如果呈现模态视图控制器,则第二种解决方案不起作用。
      • 第二个 ipad 工作得很好,谢谢 :)
      【解决方案3】:

      @MaciekWrocław 快速回答版

      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)!
      
          }
      
      }
      

      【讨论】:

        【解决方案4】:

        还有一种方法

        在视图中添加如下方法会出现每次push/pop视图控制器添加的情况

        -(void)viewWillAppear:(BOOL)animated
        {
             [super viewWillAppear:animated];
        
             [[[UIApplication sharedApplication] keyWindow] addSubview:<yourViewObject>];
        }
        

        为了在下一个类中删除这个视图,添加以下代码行

        -(void)viewWillDisappear:(BOOL)animated
        {
            [super viewWillDisappear:animated];
        
            [<yourViewObject> removeFromSuperview];
         }
        

        【讨论】:

          【解决方案5】:

          如果您知道顶部是什么子视图,请使用:

          UIView* topMostSubView = <your view's top most subview>
          [self.view insertSubview:alert aboveSubview:topMostSubView];
          

          【讨论】:

          • 这是一个很大的假设。
          猜你喜欢
          • 2011-04-05
          • 2013-06-25
          • 1970-01-01
          • 2011-07-11
          • 2014-07-30
          • 1970-01-01
          • 1970-01-01
          • 2016-10-06
          • 1970-01-01
          相关资源
          最近更新 更多