Skip to content

Instantly share code, notes, and snippets.

View ioskevinshah's full-sized avatar

Kevin Shah ioskevinshah

View GitHub Profile
/// NavigationTitleDropDownButton
class NavigationTitleDropDownButton: UIButton {
/// Variable Declaration(s)
var lblTitle: UILabel = {
var lbl: UILabel = UILabel()
lbl.translatesAutoresizingMaskIntoConstraints = false
lbl.backgroundColor = .clear
lbl.textAlignment = .center
lbl.adjustsFontSizeToFitWidth = true
fileprivate func addSubView() {
verticalStackView.addArrangedSubview(lblTitle)
verticalStackView.addArrangedSubview(lblSubTitle)
horizontalStackView.addArrangedSubview(verticalStackView)
horizontalStackView.addArrangedSubview(imgVDropDown)
addSubview(horizontalStackView)
}
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
}()
var imgVDropDown: UIImageView = {
var imgV: UIImageView = UIImageView(image: UIImage(named: "ic_dropDown")?.withRenderingMode(.alwaysTemplate))
imgV.tintColor = .blue
return imgV
}()
func prepareNavigationBar() {
navigationButtonView.addTarget(self, action: #selector(handleNavigationTitleSelection(_:)), for: .touchUpInside)
navigationButtonView.configure("Kevin Shah", subTitle: "iOS Developer")
navigationItem.titleView = navigationButtonView
}
/// Variable Declaration(s)
var navigationButtonView: NavigationTitleDropDownButton = NavigationTitleDropDownButton()
// 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)
}
}
fileprivate func prepareUIView() {
/// Adding SubView(s)
addSubView()
/// Preparing constraint(s)
prepareConstraintForHorizontalStackView()
}
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
}
fileprivate func addSubView() {
verticalStackView.addArrangedSubview(lblTitle)
verticalStackView.addArrangedSubview(lblSubTitle)
addSubview(verticalStackView)
}