如何更改状态栏背景颜色和文本颜色在iOS 7?如何更改状态栏背景颜色和文本颜色在iOS 7?(How

2019-05-08 19:35发布

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

导航栏具有橙色和状态栏具有白色文字颜色黑色背景颜色。 然而,当我运行在iOS 7的同一个应用程序,我观察状态栏看起来透明具有相同的橙色背景色导航栏和状态栏文本颜色为黑色。

由于这个我不能在状态栏和导航栏之间进行区分。

如何让我的状态栏看起来是一样的,因为它在的iOS 5和6,这是黑色背景色和白色的文本颜色? 我怎样才能做到这一点编程?

Answer 1:

我不得不尝试寻找其他方式。 不涉及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为我工作。

NB我们在应用程序商店这一逻辑的应用程序。 所以我想它是好的,与App Store的政策。


编辑:

使用您自己的风险。 形成评议@Sebyddd

我曾拒绝了这项事业的一个应用程序,而另一个被接受就好了。 他们认为这是私人API的使用,所以你是在审查过程:)受到运气 - Sebyddd



Answer 2:

转到您的应用程序info.plist

1)设置View controller-based status bar appearance ,以NO
2)设置Status bar style ,以UIStatusBarStyleLightContent

然后转到您的应用程序代理并粘贴下面的代码,您设置的Windows的RootViewController的。

#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v)  ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)

if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0"))
{
    UIView *view=[[UIView alloc] initWithFrame:CGRectMake(0, 0,[UIScreen mainScreen].bounds.size.width, 20)];
    view.backgroundColor=[UIColor blackColor];
    [self.window.rootViewController.view addSubview:view];
}

希望能帮助到你。



Answer 3:

1)设置UIViewControllerBasedStatusBarAppearance为YES在plist中

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

3)添加下面的方法:

 -(UIStatusBarStyle)preferredStatusBarStyle{ 
    return UIStatusBarStyleLightContent; 
 } 

更新:
还要检查开发商引导到最IOS-7-状态栏



Answer 4:

虽然在处理的iOS 7状态栏的背景色,有2例

案例1:查看与导航栏

在这种情况下使用下面的代码在您的viewDidLoad方法

 UIApplication *app = [UIApplication sharedApplication];
 CGFloat statusBarHeight = app.statusBarFrame.size.height;

 UIView *statusBarView = [[UIView alloc] initWithFrame:CGRectMake(0, -statusBarHeight, [UIScreen mainScreen].bounds.size.width, statusBarHeight)];
 statusBarView.backgroundColor = [UIColor yellowColor];
 [self.navigationController.navigationBar addSubview:statusBarView];

案例2:查看无导航栏

在这种情况下使用下面的代码在您的viewDidLoad方法

 UIApplication *app = [UIApplication sharedApplication];
 CGFloat statusBarHeight = app.statusBarFrame.size.height;

 UIView *statusBarView =  [[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, statusBarHeight)];
 statusBarView.backgroundColor  =  [UIColor yellowColor];
 [self.view addSubview:statusBarView];

源链接http://code-ios.blogspot.in/2014/08/how-to-change-background-color-of.html



Answer 5:

在iOS系统7的状态栏不具有背景,因此,如果你把一个黑色的20px的高视图背后,你会得到相同的结果作为iOS 6中。

您可能还需要阅读的iOS 7 UI转换指南关于这一主题的更多信息。



Answer 6:

您可以在应用程序启动或您的视图控制器的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
    }

}



下面是结果:


下面是苹果准则/指令有关状态栏的变化。 只有黑暗与光(而与黑)被允许在状态栏中。

这里是-如何更改状态栏样式:

如果你想设置状态栏样式,应用的水平,那么设置UIViewControllerBasedStatusBarAppearanceNO在`的.plist”文件。

如果你婉设置状态栏样式,在视图控制器的水平,那么请按照下列步骤操作:

  1. 设置UIViewControllerBasedStatusBarAppearanceYES.plist文件,如果你需要在只的UIViewController级别设置状态栏样式。
  2. 在viewDidLoad中添加功能- setNeedsStatusBarAppearanceUpdate

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

-

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

override var preferredStatusBarStyle: UIStatusBarStyle {
    return .lightContent
}


Answer 7:

在您的viewDidLoad方法中这样写:

if ([self respondsToSelector:@selector(setEdgesForExtendedLayout:)]) {
    self.edgesForExtendedLayout=UIRectEdgeNone;
    self.extendedLayoutIncludesOpaqueBars=NO;
    self.automaticallyAdjustsScrollViewInsets=NO;
}

它固定的状态栏的颜色,我和其他UI错位也给一个程度。



Answer 8:

只需添加到沙希德的答案 - 你可以考虑方向变化或不同的设备使用这个(iOS7 +):

- (BOOL) application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
  ...

  //Create the background
  UIView* statusBg = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.window.frame.size.width, 20)];
  statusBg.backgroundColor = [UIColor colorWithWhite:1 alpha:.7];

  //Add the view behind the status bar
  [self.window.rootViewController.view addSubview:statusBg];

  //set the constraints to auto-resize
  statusBg.translatesAutoresizingMaskIntoConstraints = NO;
  [statusBg.superview addConstraint:[NSLayoutConstraint constraintWithItem:statusBg attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:statusBg.superview attribute:NSLayoutAttributeTop multiplier:1.0 constant:0.0]];
  [statusBg.superview addConstraint:[NSLayoutConstraint constraintWithItem:statusBg attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:statusBg.superview attribute:NSLayoutAttributeLeft multiplier:1.0 constant:0.0]];
  [statusBg.superview addConstraint:[NSLayoutConstraint constraintWithItem:statusBg attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:statusBg.superview attribute:NSLayoutAttributeRight multiplier:1.0 constant:0.0]];
  [statusBg.superview addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[statusBg(==20)]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(statusBg)]];
  [statusBg.superview setNeedsUpdateConstraints];
  ...
}


Answer 9:

为背景,你可以很容易地添加视图,类似例子:

 UIView *view=[[UIView alloc] initWithFrame:CGRectMake(0, 0,320, 20)];
    view.backgroundColor=[UIColor colorWithRed:0/255.0 green:0/255.0 blue:0/255.0 alpha:0.1];
    [navbar addSubview:view];

其中,“导航栏”是一个UINavigationBar的。

我希望它可以帮助你!



Answer 10:

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

绝对正确的解释

每一个问题涉及。

与感谢Warif Akhand仙人 !

对于有关的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
}


Answer 11:

更改状态栏的背景色:斯威夫特:

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


Answer 12:

斯威夫特4: -

//更改状态栏背景颜色

    let statusBar = UIApplication.shared.value(forKeyPath: "statusBarWindow.statusBar") as? UIView

    statusBar?.backgroundColor = UIColor.red


Answer 13:

在迅速2.0在iOS 9的情况下

放置在应用程序的委托下,在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)


Answer 14:

iTroid23解决方案为我工作。 我错过了迅速解决。 因此,也许这是有益的:

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

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

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

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

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


Answer 15:

我成功了自定义状态栏的颜色很简单,将在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/



Answer 16:

对于栏的颜色:您提供的栏自定义背景图片。

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



Answer 17:

如果您使用的是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)中,当你想隐藏导航栏,否则这是用作背景仍然可以看到该视图扩展方法。



Answer 18:

请试试这个。 在你的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]; }



Answer 19:

斯威夫特4

Info.plist添加该属性

基于控制器的视图状态栏外观NO

之后,在AppDelegatedidFinishLaunchingWithOptions添加几行代码

UIApplication.shared.isStatusBarHidden = false
UIApplication.shared.statusBarStyle = .lightContent


Answer 20:

在斯威夫特5和Xcode的10.2

DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + Double(Int64(0.1 * Double(NSEC_PER_SEC))) / Double(NSEC_PER_SEC), execute: {

//Set status bar background colour
let statusBar = UIApplication.shared.value(forKeyPath: "statusBarWindow.statusBar") as? UIView
statusBar?.backgroundColor = UIColor.red
//Set navigation bar subView background colour
   for view in controller.navigationController?.navigationBar.subviews ?? [] {
      view.tintColor = UIColor.white
      view.backgroundColor = UIColor.red
   }
})

在这里,我固定的状态栏的背景色和导航栏的背景颜色。 如果你不想导航栏的颜色评论它。



文章来源: How to change the status bar background color and text color on iOS 7?