Overview
Hello iOS Developer, we all want our custom module which helps us dynamically and in easy of implementing with our purpose. Here image, show what we going to learn in this tutorial.![]() |
Custom Navigation Bar: Swift 4.2 |
I'm assuming that you are familiar with project setup. Open Storyboard, an embed navigation controller to view controller. Create a new swift named it "NavigationTitleViewDropdown". Now here we are going to create a class with the same name as a swift file, a superclass will be UIButton because we required tap event handling.
Now we are going to create some variables. First one is label title a compute variable here we are going to change some property values as per our need.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// Variable Declaration(s) | |
var lblTitle: UILabel = { | |
var lbl: UILabel = UILabel() | |
lbl.translatesAutoresizingMaskIntoConstraints = false | |
lbl.backgroundColor = .clear | |
lbl.textAlignment = .center | |
lbl.adjustsFontSizeToFitWidth = true | |
lbl.minimumScaleFactor = 0.75 | |
lbl.textColor = .darkText | |
lbl.font = .boldSystemFont(ofSize: UIFont.systemFontSize) | |
return lbl | |
}() |
Thus the same goes for label subtitle with some minor modification.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var lblSubTitle: UILabel = { | |
var lbl: UILabel = UILabel() | |
lbl.translatesAutoresizingMaskIntoConstraints = false | |
lbl.backgroundColor = .clear | |
lbl.textAlignment = .center | |
lbl.adjustsFontSizeToFitWidth = true | |
lbl.minimumScaleFactor = 0.75 | |
lbl.textColor = .darkGray | |
lbl.font = .systemFont(ofSize: UIFont.smallSystemFontSize) | |
return lbl | |
}() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fileprivate var verticalStackView: UIStackView = { | |
let stackView: UIStackView = UIStackView() | |
stackView.translatesAutoresizingMaskIntoConstraints = false | |
stackView.isUserInteractionEnabled = false | |
stackView.alignment = .center | |
stackView.axis = .vertical | |
stackView.distribution = .equalCentering | |
return stackView | |
}() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
override init(frame: CGRect) { | |
super.init(frame: frame) | |
/// Preparing UIView | |
prepareUIView() | |
} |
For adding subviews we are going to create the method of it. In this gone add all subview.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fileprivate func addSubView() { | |
verticalStackView.addArrangedSubview(lblTitle) | |
verticalStackView.addArrangedSubview(lblSubTitle) | |
addSubview(verticalStackView) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fileprivate func prepareConstraintForHorizontalStackView() { | |
verticalStackView.centerXAnchor.constraint(equalTo: centerXAnchor).isActive = true | |
verticalStackView.centerYAnchor.constraint(equalTo: centerYAnchor).isActive = true | |
verticalStackView.widthAnchor.constraint(equalTo: widthAnchor, multiplier: 1).isActive = true | |
verticalStackView.heightAnchor.constraint(equalTo: heightAnchor, multiplier: 1).isActive = true | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fileprivate func prepareUIView() { | |
/// Adding SubView(s) | |
addSubView() | |
/// Preparing constraint(s) | |
prepareConstraintForHorizontalStackView() | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// MARK: - Configuartion Related Method(s) | |
extension NavigationTitleDropDownButton { | |
func configure(_ title: String, subTitle: String? = nil) { | |
lblTitle.text = title | |
lblSubTitle.text = subTitle | |
lblSubTitle.isHidden = (subTitle?.isEmpty == true) | |
} | |
} |
Run the application, go and press title-view. It will print statement in the console. Only a couple of thing is left to do over here. Adding drop-down arrow image and high-lighting title-view.
For adding the image drop-down we need to create a variable of it.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var imgVDropDown: UIImageView = { | |
var imgV: UIImageView = UIImageView(image: UIImage(named: "ic_dropDown")?.withRenderingMode(.alwaysTemplate)) | |
imgV.tintColor = .blue | |
return imgV | |
}() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fileprivate var horizontalStackView: UIStackView = { | |
let stackView: UIStackView = UIStackView() | |
stackView.translatesAutoresizingMaskIntoConstraints = false | |
stackView.isUserInteractionEnabled = false | |
stackView.alignment = .center | |
stackView.axis = .horizontal | |
stackView.distribution = .equalCentering | |
stackView.spacing = 4 | |
return stackView | |
}() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fileprivate func addSubView() { | |
verticalStackView.addArrangedSubview(lblTitle) | |
verticalStackView.addArrangedSubview(lblSubTitle) | |
horizontalStackView.addArrangedSubview(verticalStackView) | |
horizontalStackView.addArrangedSubview(imgVDropDown) | |
addSubview(horizontalStackView) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fileprivate func addSubView() { | |
verticalStackView.addArrangedSubview(lblTitle) | |
verticalStackView.addArrangedSubview(lblSubTitle) | |
horizontalStackView.addArrangedSubview(verticalStackView) | |
horizontalStackView.addArrangedSubview(imgVDropDown) | |
addSubview(horizontalStackView) | |
} |
Full Video:
Feel free to comment and sharing.
Wow, amazing blog layout! How long have you been blogging for? you make blogging look easy. The overall look of your website is fantastic, let alone the content!
ReplyDelete3D Animation Services in Atlanta
Chatbot Development Company
Mobile app development in Coimbatore
Thanks
DeleteNow year has been completed, If you like content, Please share and let me know. So, it will be motivate to post more content like this. Thanks
ReplyDeleteNice Blog.
ReplyDeleteI had a look at your blog in detail and seems that it is very useful for all the users who are looking to understand about Custom Navigation Bar with Swift. I was looking for swift application development company
Thanks, @Synsoft Global
DeletePlease accept our apologies for the unintended delay.
ReplyDeleteWhich need to be recorrect by owner.
--> Feel free to share your suggestions.
Thanks
This comment has been removed by the author.
ReplyDeleteThanks
DeleteHotel and Casino: Atlantic City, NJ - Mapyro
ReplyDeleteMapyro Resort and Casino 정읍 출장샵 offers 5500 rooms and 평택 출장마사지 suites. 경기도 출장마사지 Featuring 1,500 rooms and suites, this 원주 출장안마 hotel is located in Atlantic City, NJ. Rating: 4.5 · 8 나주 출장샵 reviews