如何使用swift在一个视图控制器中更改状态栏颜色?

时间:2016-01-28 12:58:12

标签: ios xcode swift

List<int> uncheckedID = new List<int>();

public Form2()
{
    InitializeComponent();
    gridControl1.DataSource = CreateTable(10);

}     
private static DataTable CreateTable(int RowCount)
{
    Random rnd = new Random();
    DataTable tbl = new DataTable();
    tbl.Columns.Add("Name", typeof(string));
    tbl.Columns.Add("ID", typeof(int));
    tbl.Columns.Add("Number", typeof(int));
    tbl.Columns.Add("Date", typeof(DateTime));
    for (int i = 0; i < RowCount; i++)
        tbl.Rows.Add(new object[] { String.Format("Name{0}", i), i, i % 3, DateTime.Now.AddHours(i) });
    return tbl;
}  
private void gridView1_CustomDrawRowFooterCell(object sender, DevExpress.XtraGrid.Views.Grid.FooterCellCustomDrawEventArgs e)
{
    e.Info.DisplayText = "Total value for " + gridView1.GetGroupRowValue(e.RowHandle).ToString() + " = " + e.Info.Value;
}

private void gridView1_ShownEditor(object sender, EventArgs e)
{
    ColumnView view = (ColumnView)sender;
    if (view.FocusedColumn.FieldName == "Number" && view.ActiveEditor is RadioGroup)
    {
        RadioGroup edit = (RadioGroup)view.ActiveEditor;
        //You can add the items at run time.
        edit.Properties.Items.AddRange(new DevExpress.XtraEditors.Controls.RadioGroupItem[] 
        {
            new DevExpress.XtraEditors.Controls.RadioGroupItem(0, "Item 1"),
            new DevExpress.XtraEditors.Controls.RadioGroupItem(1, "Item 2"),
            new DevExpress.XtraEditors.Controls.RadioGroupItem(2, "Item 3")
        });
    }
}

我使用这个在所有应用中将状态栏更改为亮起。但现在我需要在一个View Controller中将其更改为黑色。我怎么能这样做?

10 个答案:

答案 0 :(得分:12)

将project.plist中的View controller-based status bar appearance设置为

使用viewWillAppearviewWillDisappear设置和重置statusBarStyle,同时保留一个属性与之前的statusBarStyle一样

let initialStatusBarStyle : UIStatusBarStyle

func viewWillAppear(animated: Bool) {
    super.viewWillAppear(animated)
    initialStatusBarStyle = UIApplication.sharedApplication().statusBarStyle
    UIApplication.sharedApplication().setStatusBarStyle(.LightContent, animated: animated)
}

func viewWillDisappear(animated: Bool) {
    super.viewWillDisappear(animated)
    UIApplication.sharedApplication().setStatusBarStyle(initialStatusBarStyle, animated: animated)
}

答案 1 :(得分:10)

Xcode 8.1,带有@IBDesignable的Swift 3解决方案

这个解决方案有点不同:

  • UIViewController的子类集中逻辑
  • 没有viewDidLoad或viewDidDisappear的代码
  • 使用@IBDesignable,以便您可以在故事板上的“属性”检查器中设置状态栏颜色

第1步 - 设置Info.plist文件

Info.plist

第2步 - 子类UIViewController

import UIKit

@IBDesignable
class DesignableViewController: UIViewController {

    @IBInspectable var LightStatusBar: Bool = false

    override var preferredStatusBarStyle: UIStatusBarStyle {
        get {
            if LightStatusBar {
                return UIStatusBarStyle.lightContent
            } else {
                return UIStatusBarStyle.default
            }
        }
    }
}

第3步 - 从DesignableViewController继承

从以下位置更改ViewController的代码:

class ViewController: UIViewController {

要:

class ViewController: DesignableViewController {

第4步 - 在故事板上设置您的偏好

在Storyboard上选择ViewControllers并转到Attributes Inspector: Attributes Inspector

第5步 - 运行项目并测试

在我的项目中,我设置了一个带有2个视图控制器的标签栏控制器,并在两者之间来回切换。似乎对我来说工作正常。 Light Status Bar Dark Status Bar

答案 2 :(得分:6)

<强>解决:
Swift 3.1

在View Controller中使用此代码:

override var preferredStatusBarStyle: UIStatusBarStyle {
    return .lightContent
}

答案 3 :(得分:2)

Swift 3

将project.plist中基于视图控制器的状态栏外观设置为NO

override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)
    UIApplication.shared.setStatusBarStyle(.default, animated: animated)
}

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    UIApplication.shared.setStatusBarStyle(.lightContent, animated: animated)
}

答案 4 :(得分:1)

Objective-C答案:

-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
}

答案 5 :(得分:0)

 let color = UIColor(red:0.00, green:0.60, blue:0.48,alpha:1.0)
        UINavigationBar.appearance().tintColor = UIColor.blue
        UINavigationBar.appearance().barTintColor = color

self.navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.white]

答案 6 :(得分:0)

如果您从公共视图控制器派生了视图控制器,那么您可以这样做:

第1步: 将此密钥添加到您应用的info.plist文件中。

enter image description here

第2步: override这是普通视图控制器(或ParentViewController)。

override var preferredStatusBarStyle: UIStatusBarStyle {
    if self is YourChildViewController {
        return .lightContent
    }
    return .default
}

就是这样!没有更多花哨的东西。

答案 7 :(得分:0)

在导航控制器中不会被调用。导航控制器的preferredStatusBarStyle将被调用。连同您的代码一起尝试:

extension UINavigationController {

open override var preferredStatusBarStyle: UIStatusBarStyle {
    return topViewController?.preferredStatusBarStyle ?? .default
    }
}

,也写这个:

override var preferredStatusBarStyle: UIStatusBarStyle {
    return .lightContent
}

答案 8 :(得分:0)

在swit4中,基于导航栏,这在我的项目中工作正常

   let app = UIApplication.shared
   let statusBarHeight: CGFloat = app.statusBarFrame.size.height

   let statusbarView = UIView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: statusBarHeight))
   statusbarView.backgroundColor = UIColor.red
   view.addSubview(statusbarView)

答案 9 :(得分:0)

您可以使用以下代码设置状态栏颜色及其对我的作用

self.navigationController?.SetStatusBar(StatusBarbackgroundColor: ThemeBackColor, StatusTextColor: .black)

谢谢 快乐编码:)