0

I have a TableViewController and in it I created a UIView programatically that is the width and height of the status bar and has a colored background. Basically, it serves as the status bar's background:

let statusBarBG = UIView(frame: CGRectMake(0, 0, widthOfScreen, 20))
statusBarBG.backgroundColor = UIColor...
self.view.addSubView(statusBarBG)

However, when I drag the table up and down this status bar background moves with the entire table view, as seen in the following image (on the left is how it looks normally, the bigger section of red is my nav bar's bg):

enter image description here

So my question is: how do I create a view inside this tableViewController that is more like a sibling to the tableView rather than a child that moves with it? Thanks.

3 Answers 3

3

how do I create a view inside this tableViewController that is more like a sibling to the tableView rather than a child that moves with it?

Unfortunately this is not possible with a tableviewcontroller, because the view of a tableviewcontroller is a UITableView instance, which in turn is a subclass of UIScrollView. And basically anything you put as a child of a scrollview scrolls with it.

A better approach would be to move away from using a tableviewcontroller, and use a normal view controller with an embedded table view instead. You can even reuse your view controller by just adding it to the heirarchy.

Another (easier) approach is to handle the scroll delegate method of the uitableview, and adjust the header view's Y position according to the offset.

Sign up to request clarification or add additional context in comments.

1 Comment

Makes sense. My program kept crashing when I tried embedding a UITableView inside ViewController for some reason (even though I dragged the delegate and data source to the controller), but I'll have to figure that out. I'll give it a shot, thanks.
1

You could just use an image if you only want that view to act like a background color.

[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"navigationbar.png"] forBarMetrics:UIBarMetricsDefault];

or use imageWithColor if you want just a color instead of a custom image... In your pics it looks like you want the whole nav bar + status bar just red... So this code is for the whole nav bar + status bar... If you want the status bar different just design an image and use that code... i m not a swift guy sorry, but it should be very similar...

EDIT: ah you don t use that tableView in a navigation controller? then it might not work...

EDIT 2:

this is my image enter image description here

with this code

[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"navigationbar.png"] forBarMetrics:UIBarMetricsDefault];

i get

enter image description here

without it s just clean

enter image description here

3 Comments

I changed my TableViewController to a normal ViewController, as detailed in the answer by maranas, and created a UIView to serve as the status bar background and it all works fine. However, your method might be a better option, since yes, my views are embedded in a NavigationController. The issue with that was that the nav bar didn't stretch to the top of the page, above the status bar, so that top rectangle was white (uncolored). Would setting its image allow me to reach that area as opposed to just setting the background color?
I m setting just one image (see my code) and yes it covers all, the nav bar and the stauts bar above... well this is for iOS7 and iOS8.. not sure but i think apple changed some things with nav bar and status bar in iOS7 in comparison to iOS6
p.s. what i learned though is that using a tableviewcontroller is just dumb and useless because you can t change anything like if you attach an ad banner at the button of a tableview you can t resize the table to make space for the ad banner and the last row of the table is under the ad banner and is not touchable... this is just one example so it s always better to have a normal view controller and just place a table in it instaed of using a tableview controller... just my experience...
0

Try this:

    let bottomView = UIView()
    bottomView.backgroundColor = .red // or your color
    bottomView.frame = CGRect(x: 0, y: UIScreen.main.bounds.size.height - 78, width: tableView.frame.size.width, height: 78) // 78 or your size of view
    navigationController?.view.addSubview(bottomView)
    tableView.tableFooterView = UIView()

enter image description here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.