关闭第二个UIWindow后恢复状态栏状态

时间:2017-07-12 12:04:50

标签: ios swift uiwindow

UIWindow包含MainViewController lightContent preferredStatusBarStyle UIWindow。我已创建第二个PopupViewController实例以显示default,其中preferredStatusBarStyle用作UIWindow

当我显示PopupViewController状态栏样式更改为default的第二个lightContent时,但是当我隐藏它时,样式不会更改回UIWindow

同样的问题适用于弹出窗口中有VC隐藏状态栏的情况 - 弹出窗口被取消时状态栏不显示。

// Prepare window to show dialog box in newWindow = UIWindow(frame: UIScreen.main.bounds) newWindow?.windowLevel = 3 // Overlay new window newWindow?.makeKeyAndVisible() self.mainWindow.windowLevel = 1 self.mainWindow.endEditing(true) newWindow?.isHidden = false // Display dialog newWindow?.rootViewController = PopupViewController() 创作:

UIWindow

UIView.animate( withDuration: 1.0, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 0, options: .curveEaseOut, animations: { [weak self] in self?.newWindow?.alpha = 0 }, completion: { [weak self] _ in self?.newWindow?.windowLevel = 0 self?.newWindow?.rootViewController = nil self?.newWindow?.alpha = 1 self?.mainWindow.makeKeyAndVisible() } ) 解雇:

public override UIView GetViewForHeader(UITableView tableView, nint section)
{
    var headerView = new UIView(new CGRect(0, 0, (float)tableView.Bounds.Width, 70));

    var label = new UILabel(new CGRect(15, 35, (float)tableView.Bounds.Width - 100, 15));
    label.TextColor = UIColor.Gray;
    label.Font = UIFont.FromName("Helvetica", 12f);
    label.Text = @"Sample Title"; //sectionHeaders[Convert.ToInt32(section)];
    headerView.AddSubview(label);

    var bottomBorder = new UIView(new CGRect(0, 69, (float)tableView.Bounds.Width, 1));
    bottomBorder.BackgroundColor = UIColor.Red;
    headerView.AddSubview(bottomBorder);

    return headerView;
} 

谢谢!

编辑:弹出窗口可以随时出现,我不知道当时哪个VC处于活动状态

4 个答案:

答案 0 :(得分:0)

更改ViewWillAppear上的状态栏样式

override func viewWillAppear(_ animated: Bool) {
    UIApplication.shared.statusBarStyle = .lightContent
}

答案 1 :(得分:0)

  1. 请在您更改状态栏颜色Light的第一个控制器上添加以下代码。

    覆盖func viewDidAppear(_ animated:Bool){UIApplication.shared.setStatusBarStyle(UIStatusBarStyle.lightContent,animated:false)}

  2. 现在在第二个控制器中,您需要默认状态栏样式,请输入代码:

    覆盖func viewDidAppear(_ animated:Bool){UIApplication.shared.setStatusBarStyle(UIStatusBarStyle.default,animated:false)}

  3. 您肯定会通过上面的代码获得正确的解决方案

答案 2 :(得分:0)

针对您案例的简单解决方案

  1. 显示窗口
  2. 针对您案例的简单解决方案

    1. 新的UIWindow创建:

      // Prepare window to show dialog box in
      newWindow = UIWindow(frame: UIScreen.main.bounds)
      newWindow?.windowLevel = 3
      
      // Overlay new window
      newWindow?.makeKeyAndVisible()
      self.mainWindow.windowLevel = 1
      self.mainWindow.endEditing(true)
      newWindow?.isHidden = false
      
      // Display dialog
      newWindow?.rootViewController = PopupViewController()
      // Now you can change status bar style default or other style
      UIApplication.shared.statusBarStyle = .default
      
    2. 新的UIWindow解雇:

      UIView.animate(
          withDuration: 1.0,
          delay: 0,
          usingSpringWithDamping: 1,
          initialSpringVelocity: 0,
          options: .curveEaseOut,
          animations: { [weak self] in
              self?.newWindow?.alpha = 0
              // Revert back to default
              UIApplication.shared.statusBarStyle = .lightContent
          },
          completion: { [weak self] _ in
              self?.newWindow?.windowLevel = 0
              self?.newWindow?.rootViewController = nil
              self?.newWindow?.alpha = 1
              self?.mainWindow.makeKeyAndVisible()
          }
      )
      

答案 3 :(得分:0)

我正在寻找的是UIViewController.setNeedsStatusBarAppearanceUpdate()。告诉VC状态栏外观已更改并需要恢复是方便的方法。

// make main window key but transparent
self.mainWindow.alpha = 0
self.newWindow?.windowLevel = 0
self.newWindow?.alpha = 1
self.mainWindow.makeKey()

// restore status bar appearance  
self.mainWindow.rootViewController!.setNeedsStatusBarAppearanceUpdate()

// Fade in main window with (status bar is in proper state at this moment)
UIView.animate(
        withDuration: 0.9,
        delay: 0,
        usingSpringWithDamping: 1,
        initialSpringVelocity: 0,
        options: .curveEaseIn,
        animations: { [weak self] in
            self?.mainWindow.alpha = 1
        },
        completion: { [weak self] _ in
            // destroy popup VC
            self?.newWindow?.rootViewController = nil
        }
)

Here is useful article on this subject

谢谢大家!