0

I wanted to get a specific colour to the iOS status bar ,I understand it will take the colour of the top view controllers view . And hence I can get this by applying the colour I want for the status bar to the table view ,which would look like this

enter image description here

But when it doesn't have any rows then whole table view gets that color (Obvious! but thats not what i want)

enter image description here

Is there any way to give colour only to the top part of the tableview so that status bar gets that colour or can i change the status bar color alone . I have

View controller-based status bar appearance

in Info.plist as NO .

4 Answers4

1

you should take one custom view in place of status bar and give background color to that view. and take your tableview just below this custom view so you not need to set color on tableview's upper part!!

View's height should be 20 and width should equal to screen width.

and position should be (0,0).

Hope this will help :)

Ketan Parmar
  • 27,092
  • 9
  • 50
  • 75
  • where should I add the custom view ? `self.tableView.addSubview(topView)` or `self.tableView.superView?.addSubview(topView)` – rajesh sukumaran May 11 '16 at 07:00
  • nop. in your view controller. add customview with frame (0,0,self.view.frame.size.width,20) and your tableview's frame should (0,20,width,height) – Ketan Parmar May 11 '16 at 07:04
1

You can apply background color to status bar like following

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

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

Using the above code in AppDelegate didFinishLaunchingWithOptions method would change the status bar background color of entire app. If you want to change the background color to specific view controllers, then you should follow the @Lion's approach.

Hope this helps.

Surya Subenthiran
  • 2,217
  • 1
  • 15
  • 22
0

You can also add one window to top status bar and change its color. It will look like this:

// AppDelegate.Swift
let topWindow = UIWindow(frame: CGRect(....))
topWindow.backgroundColor = ...
window.addSubview(topWindow)
Lumialxk
  • 6,239
  • 6
  • 24
  • 47
0

On Swift:

override func viewWillAppear(animated: Bool) {
    self.navigationController?.navigationBarHidden =  true

UIApplication.sharedApplication().statusBarHidden = false
UIApplication.sharedApplication().statusBarStyle = .LightContent

let statusBar: UIView = UIApplication.sharedApplication().valueForKey("statusBar") as! UIView
if statusBar.respondsToSelector("setBackgroundColor:") {
    statusBar.backgroundColor = UIColor.redColor()
}

}
Alvin George
  • 14,148
  • 92
  • 64