Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a custom navigation bar in Swift Xcode?

I am working on a chat app for my graduation project in iOS. I designed the following navigation bar:

enter image description here

Now I am trying to develop above graphic in my Xcode project, but I don't know if this is the correct way to achieve this and doesn't get it like the graphic.

I am using a xib file, that I load in my ViewController.swift using an instance of it. Than add it as the titleView of the navigationItem:

let helperView = HelperView()
navigationItem.titleView = helperView

This is the result of above code snippet:

enter image description here

The problem of this result is that it is overlapping the left bar button item and another problem, that I still doesn't figured out is, if the message bubble can have a dynamic height, when it have multiple lines (max is 3 lines).

enter image description here

Does anyone have experience with this kind of design within Xcode and is this the correct way to do this, or is there a better way to achieve this. Maybe a custom UINavigationController class?

like image 757
Caspert Avatar asked Oct 31 '17 10:10

Caspert


People also ask

How do I create a navigation bar in swift 5?

Steps for creating a custom viewCreate a UIView outlet in NavigationBar. swift , linking it to the top level view in the . xib file (named it “view” for convenience). Then add other outlets to other controls in the .

How do I add a navigation bar button in Swift?

You need to open the storyboard, delete the view controller that you have, press cmd , shift , l , and then search for navigation controller . Drag that onto the storyboard. You now need to click on the navigation controller and set it to be the is initial view controller under the attributes inspector .


1 Answers

Try create the whole navigation view that you desire, that mean the width is equal to the view, then try use this code to add it

    navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
    navigationController?.navigationBar.shadowImage = UIImage()
    navigationController?.navigationBar.backgroundColor = .clear

    navigationController?.view.insertSubview(subview, belowSubview: navigationController?.navigationBar)

It will makes your navigation bar become invisible but still showing the bar button

Update, by dimpiax

But better to override your UINavigationController class, and setup view in viewDidLoad

navigationBar.shadowImage = UIImage()
navigationBar.setBackgroundImage(UIImage(), for: .default)
navigationBar.backgroundColor = .clear

And on dependent viewController's view – show specific view.

like image 197
Tj3n Avatar answered Sep 28 '22 08:09

Tj3n