Search code examples
iosxcodeswiftautolayoutuistoryboard

How to create a fixed navigation bar iOS Swift


I want to create a fixed header that doesn't scroll with tableViewCells, or other content that I have. The header should stay in place fixed, just like in most iOS apps.

Here is the image of the issue I'm facing:

enter image description here

I tried embedding it in a NavigationController & a TabViewController. That didn't work.

I also tried adding a scroll-view.

I referred to this link but it's outdated and didn't help: how iphone facebook app make the navigation bar fixed

Thanks.


Solution

  • I'd really recommend fooling around with the Navigation controller until you figure it out, it's really the best method for this. If you've embedded it, I'd check out your view with the Attributes inspector, to make sure everything needed is enabled.

    HOWEVER, there is another way I've used before. Create a new ViewController, and create two views inside of it. One will be your FakeNavBar, and the other will be a container/TableView that'll hold your data.

    Once you've added both these to your new VC, just set them up normally, and bingo! One tip for this, is that NavBar is typically 64 points high, so your fake bar will be something like this:

    fakeNavBar.frame = CGRectMake(0, 0, theWidth, 64)
    tableView.frame = CGRectMake(0, 64, theWidth, theHeight-64)
    

    And your view hierarchy will look like this: enter image description here