ios状态栏变黑 - ios状态栏透明




如何更改iOS 7上的状态栏背景颜色和文本颜色? (12)

1)在plist中将UIViewControllerBasedStatusBarAppearance设置为YES

2)在viewDidLoad中做一个[self setNeedsStatusBarAppearanceUpdate];

3)添加以下方法:

 -(UIStatusBarStyle)preferredStatusBarStyle{ 
    return UIStatusBarStyleLightContent; 
 } 

更新:
还要检查developers-guide-to-the-ios-7-status-bar

我目前的应用程序运行在iOS 5和6上。

导航栏的颜色为橙色,状态栏的背景颜色为黑色,文本颜色为白色。 但是,当我在iOS 7上运行相同的应用程序时,我发现状态栏看起来很透明,导航栏的橙色背景颜色和状态栏文本颜色都是黑色。

由于这个原因,我无法区分状态栏和导航栏。

如何让状态栏看起来与iOS 5和6中的状态栏相同,即黑色背景颜色和白色文本颜色? 我怎样才能以编程方式做到这一点?


iTroid23解决方案为我工作。 我错过了Swift解决方案。 所以这可能有帮助:

1)在我的plist中,我不得不补充一点:

<key>UIViewControllerBasedStatusBarAppearance</key>
<true/>

2)我不需要调用“setNeedsStatusBarAppearanceUpdate”。

3)在迅速,我不得不添加到我的UIViewController:

override func preferredStatusBarStyle() -> UIStatusBarStyle {
    return UIStatusBarStyle.LightContent
}

在iOS 7中,状态栏没有背景,因此如果您在背后放置黑色20像素高的视图,则会获得与iOS 6相同的效果。

您也可以阅读iOS 7用户界面过渡指南 ,了解有关该主题的更多信息。


在iOS 9上使用swift 2.0

在didFinishLaunchingWithOptions下面的应用程序委托中放置以下代码:

    let view: UIView = UIView.init(frame: CGRectMake(0, 0, UIScreen.mainScreen().bounds.size.width, 20))

    view.backgroundColor = UIColor.blackColor()  //The colour you want to set

    view.alpha = 0.1   //This and the line above is set like this just if you want 
                          the status bar a darker shade of 
                          the colour you already have behind it.

    self.window!.rootViewController!.view.addSubview(view)

如果您使用的是UINavigationController ,则可以使用如下所示的扩展名:

extension UINavigationController {
    private struct AssociatedKeys {
        static var navigationBarBackgroundViewName = "NavigationBarBackground"
    }

    var navigationBarBackgroundView: UIView? {
        get {
            return objc_getAssociatedObject(self,
                                        &AssociatedKeys.navigationBarBackgroundViewName) as? UIView
        }
        set(newValue) {
             objc_setAssociatedObject(self,
                                 &AssociatedKeys.navigationBarBackgroundViewName,
                                 newValue,
                                 .OBJC_ASSOCIATION_RETAIN)
        }
    }

    func setNavigationBar(hidden isHidden: Bool, animated: Bool = false) {
       if animated {
           UIView.animate(withDuration: 0.3) {
               self.navigationBarBackgroundView?.isHidden = isHidden
           }
       } else {
           navigationBarBackgroundView?.isHidden = isHidden
       }
    }

    func setNavigationBarBackground(color: UIColor, includingStatusBar: Bool = true, animated: Bool = false) {
        navigationBarBackgroundView?.backgroundColor = UIColor.clear
        navigationBar.backgroundColor = UIColor.clear
        navigationBar.barTintColor = UIColor.clear

        let setupOperation = {
            if includingStatusBar {
                self.navigationBarBackgroundView?.isHidden = false
                if self.navigationBarBackgroundView == nil {
                    self.setupBackgroundView()
                }
                self.navigationBarBackgroundView?.backgroundColor = color
            } else {
                self.navigationBarBackgroundView?.isHidden = true
                self.navigationBar.backgroundColor = color
            }
        }

        if animated {
            UIView.animate(withDuration: 0.3) {
                setupOperation()
            }
        } else {
            setupOperation()
        }
    }

    private func setupBackgroundView() {
        var frame = navigationBar.frame
        frame.origin.y = 0
        frame.size.height = 64

        navigationBarBackgroundView = UIView(frame: frame)
        navigationBarBackgroundView?.translatesAutoresizingMaskIntoConstraints = true
        navigationBarBackgroundView?.autoresizingMask = [.flexibleWidth, .flexibleBottomMargin]

        navigationBarBackgroundView?.isUserInteractionEnabled = false

        view.insertSubview(navigationBarBackgroundView!, aboveSubview: navigationBar)
    }
}

它基本上使导航栏背景透明,并使用另一个UIView作为背景。 您可以调用导航控制器的setNavigationBarBackground方法,将导航栏背景颜色与状态栏一起设置。

请记住,如果要隐藏导航栏,则必须在扩展中使用setNavigationBar(hidden: Bool, animated: Bool)方法,否则用作背景的视图仍将可见。


对于条形颜色:您可以为条形提供自定义背景图像。

对于文本颜色:使用iOS关于文本处理中的信息


您可以在应用程序启动期间或视图控制器的viewDidLoad期间为状态栏设置背景颜色。

extension UIApplication {

    var statusBarView: UIView? {
        return value(forKey: "statusBar") as? UIView
    }

}

// Set upon application launch, if you've application based status bar
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        UIApplication.shared.statusBarView?.backgroundColor = UIColor.red
        return true
    }
}


or 
// Set it from your view controller if you've view controller based statusbar
class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        UIApplication.shared.statusBarView?.backgroundColor = UIColor.red
    }

}



结果如下:


这是关于状态栏更改的Apple指南/说明 。 状态栏只允许黑暗和光亮(同时和黑色)。

这里是 - 如何改变状态栏的风格:

如果你想设置状态栏风格,应用程序级别,然后在你的`.plist'文件中设置UIViewControllerBasedStatusBarAppearanceNO

如果您希望设置状态栏样式,请在视图控制器级别执行以下步骤:

  1. 如果需要仅在UIViewController级别设置状态栏样式,请将.plist文件中的UIViewControllerBasedStatusBarAppearance设置为YES
  2. 在viewDidLoad添加函数 - setNeedsStatusBarAppearanceUpdate

  3. 在视图控制器中覆盖preferredStatusBarStyle。

-

override func viewDidLoad() {
    super.viewDidLoad()
    self.setNeedsStatusBarAppearanceUpdate()
}

override var preferredStatusBarStyle: UIStatusBarStyle {
    return .lightContent
}

我成功地自定义了StatusBar颜色,非常简单,在方法中添加了AppDelegate.cs文件:

public override bool FinishedLaunching(UIApplication app, NSDictionary options)

下一个代码:

UIView statusBar = UIApplication.SharedApplication.ValueForKey(new NSString("statusBar")) as UIView;

if (statusBar!=null && statusBar.RespondsToSelector(new Selector("setBackgroundColor:")))
{
   statusBar.BackgroundColor = Color.FromHex(RedColorHex).ToUIColor();
}

所以你得到这样的东西:

链接: https://jorgearamirez.wordpress.com/2016/07/18/lesson-x-effects-for-the-status-bar/https://jorgearamirez.wordpress.com/2016/07/18/lesson-x-effects-for-the-status-bar/


更改状态栏的背景颜色:Swift:

let proxyViewForStatusBar : UIView = UIView(frame: CGRectMake(0, 0,self.view.frame.size.width, 20))    
        proxyViewForStatusBar.backgroundColor=UIColor.whiteColor()
        self.view.addSubview(proxyViewForStatusBar)

请试试这个。 在appdelegate类“didFinishLaunchingWithOptions”函数中使用此代码

[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent]; [application setStatusBarHidden:NO]; UIView *statusBar = [[[UIApplication sharedApplication] valueForKey:@"statusBarWindow"] valueForKey:@"statusBar"]; if ([statusBar respondsToSelector:@selector(setBackgroundColor:)]) { statusBar.backgroundColor = [UIColor blackColor]; }


这里有一个总的,复制和粘贴解决方案,一个

绝对正确的解释

涉及的每个问题。

感谢Warif Akhand Rishi!

关于keyPath statusBarWindow.statusBar的惊人发现。 好的。

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

    // handle the iOS bar!

    // >>>>>NOTE<<<<<
    // >>>>>NOTE<<<<<
    // >>>>>NOTE<<<<<
    // "Status Bar Style" refers to the >>>>>color of the TEXT<<<<<< of the Apple status bar,
    // it does NOT refer to the background color of the bar. This causes a lot of confusion.
    // >>>>>NOTE<<<<<
    // >>>>>NOTE<<<<<
    // >>>>>NOTE<<<<<

    // our app is white, so we want the Apple bar to be white (with, obviously, black writing)

    // make the ultimate window of OUR app actually start only BELOW Apple's bar....
    // so, in storyboard, never think about the issue. design to the full height in storyboard.
    let h = UIApplication.shared.statusBarFrame.size.height
    let f = self.window?.frame
    self.window?.frame = CGRect(x: 0, y: h, width: f!.size.width, height: f!.size.height - h)

    // next, in your plist be sure to have this: you almost always want this anyway:
    // <key>UIViewControllerBasedStatusBarAppearance</key>
    // <false/>

    // next - very simply in the app Target, select "Status Bar Style" to Default.
    // Do nothing in the plist regarding "Status Bar Style" - in modern Xcode, setting
    // the "Status Bar Style" toggle simply sets the plist for you.

    // finally, method A:
    // set the bg of the Apple bar to white.  Technique courtesy Warif Akhand Rishi.
    // note: self.window?.clipsToBounds = true-or-false, makes no difference in method A.
    if let sb = UIApplication.shared.value(forKeyPath: "statusBarWindow.statusBar") as? UIView {
        sb.backgroundColor = UIColor.white
        // if you prefer a light gray under there...
        //sb.backgroundColor = UIColor(hue: 0, saturation: 0, brightness: 0.9, alpha: 1)
    }

    /*
    // if you prefer or if necessary, method B:
    // explicitly actually add a background, in our app, to sit behind the apple bar....
    self.window?.clipsToBounds = false // MUST be false if you use this approach
    let whiteness = UIView()
    whiteness.frame = CGRect(x: 0, y: -h, width: f!.size.width, height: h)
    whiteness.backgroundColor = UIColor.green
    self.window!.addSubview(whiteness)
    */

    return true
}

我不得不尝试寻找其他方法。 这不涉及窗口上的addSubview 。 因为当键盘出现时我正在向上移动窗口。

目标C

- (void)setStatusBarBackgroundColor:(UIColor *)color {

    UIView *statusBar = [[[UIApplication sharedApplication] valueForKey:@"statusBarWindow"] valueForKey:@"statusBar"];

    if ([statusBar respondsToSelector:@selector(setBackgroundColor:)]) {
        statusBar.backgroundColor = color;
    }
}

迅速

func setStatusBarBackgroundColor(color: UIColor) {

    guard  let statusBar = UIApplication.sharedApplication().valueForKey("statusBarWindow")?.valueForKey("statusBar") as? UIView else {
        return
    }

    statusBar.backgroundColor = color
}

斯威夫特3

func setStatusBarBackgroundColor(color: UIColor) {

    guard let statusBar = UIApplication.shared.value(forKeyPath: "statusBarWindow.statusBar") as? UIView else { return }

    statusBar.backgroundColor = color
}

调用这个表单application:didFinishLaunchingWithOptions为我工作。

注意我们在应用商店中有这样一个应用程序。 所以我想应用程序商店的政策没问题。

编辑:

使用风险自负。 形成评论者@Sebyddd

我有一个应用程序拒绝这个原因,而另一个被接受很好。 他们确实认为它是私人API的使用,所以你在审查过程中受到运气的影响:) - Sebyddd





ios-statusbar