Custom Navigation Bar like Twitter in Swift 4 - Swift 4 Tutorials W3Schools

Hot

Post Top Ad

3 Nov 2017

Custom Navigation Bar like Twitter in Swift 4

Hello guys, in this article we will show how to create custom navigation bar as below:

First create Xcode project an save it. Then embed view controller into navigation controller.

Then add the following method, and call in viewsDidLoad() method:
func setupNavItems() {
    let titleImageView = UIImageView(image: #imageLiteral(resourceName: "title_icon"))
    titleImageView.frame = CGRect(x: 0, y: 0, width: 34, height: 34)
    titleImageView.contentMode = .scaleAspectFit
    navigationItem.titleView = titleImageView
    
    let followButton = UIButton(type: .system)
    followButton.setImage(#imageLiteral(resourceName: "follow"), for: .normal)
    followButton.frame = CGRect(x: 0, y: 0, width: 34, height: 34)
    navigationItem.leftBarButtonItem = UIBarButtonItem(customView: followButton)
    
    let searchButton = UIButton(type: .system)
    searchButton.setImage(#imageLiteral(resourceName: "search"), for: .normal)
    searchButton.frame = CGRect(x: 0, y: 0, width: 34, height: 34)
    
    let composeButton = UIButton(type: .system)
    composeButton.setImage(#imageLiteral(resourceName: "compose"), for: .normal)
    composeButton.frame = CGRect(x: 0, y: 0, width: 34, height: 34)
    navigationItem.rightBarButtonItems = [UIBarButtonItem(customView: composeButton),UIBarButtonItem(customView: searchButton)]   
}

iOS 11[Swift 4] Navigation Bar With Large Titles and LargeTitleTextAttributes - Color & Font Customization

No comments:

Post a Comment

Post Top Ad