6

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?

2 Answers 2

5

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.

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

6 Comments

So if I understand it correctly. I should create the avatar and message bubble correctly in my Xib file, than add above code in the viewDidAppear function? What should subview be?
The subview is the view you want to add, in this case its your helperView. You can add the above code in viewDidLoad, but keep the pointer variable to your helperView to resize the frame in viewDidLayoutSubview
Okay, and what do you mean with the last part about the pointer variable? Do you mean that it's frame should be resizable to it's content?
What i meant is that if you add this code in viewDidAppear it might have a flickering appear, add in viewDidLoad then it might have wrong frame, thats why you might want to declare the headerView in your view controller scope to change its frame in viewDidLayoutSubview
So the best way to add above code is in de viewdidLayoutSubview function? Sorry if I understand it wrong.
|
-1
// --------------------------------------------------------
// MARK:- Navigation SetUp
// --------------------------------------------------------

private func _navigationBarSetUp(){
    
    ///# Navigatoin bar and title #///
    
    navigationController?.navigationBar.isHidden = false
    navigationItem.title = vEditScreenName
    navigationController?.navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor : ColorTheme.white, NSAttributedString.Key.font: UIFont(descriptor: UIFontDescriptor(name: "Poppins-Regular", size: 20), size: 20)]
    self.view.backgroundColor = ColorTheme.vidBoomBrandPurple
    
    ///# Navigation bar back button #///
    
    let btnBack = UIButton(frame: CGRect(x: 0, y: 0, width: 25, height: 25))
    btnBack.setImage(UIImage(named: "icon_back_arrow"), for: .normal)
    btnBack.addTarget(self, action: #selector(_handlebtnBackTapped), for: .touchUpInside)
    navigationItem.leftBarButtonItem = UIBarButtonItem(customView: btnBack)
    
    ///# Navigation bar explore button #///
    
    let btnExport = UIButton(frame: CGRect(x: 0, y: 0, width: 80, height: 25))
    btnExport.setTitle("Export", for: .normal)
    btnExport.backgroundColor = .orange
    btnExport.layer.cornerRadius = 18
    btnExport.addTarget(self, action: #selector(_handleBtnExportTapped), for: .touchUpInside)
    navigationItem.rightBarButtonItem = UIBarButtonItem(customView: btnExport)
}


// --------------------------------------------------------
// MARK:- UIBarButtonItem Action
// --------------------------------------------------------

@objc fileprivate func _handlebtnBackTapped() {
    self.player.stop()
    navigationController?.popViewController(animated: true)
    navigationController?.navigationBar.isHidden = true
}

@objc func _handleBtnExportTapped(){
    self.player.stop()
    let svc = ShareVideoVC(nibName: "ShareVideoVC", bundle: nil)
    svc.videoString = videoString
    saveFile()
    self.navigationController?.pushViewController(svc, animated: true)
}

2 Comments

Can you add some details on how to use this code? Where should those methods be placed, do we need to manually call them from a certain location/place?
Add this code into ViewController class but out of viewdidload and call the function in viewdidload.

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.