Make text as UINavigationController back button - swift

I need to make this kind of navigation bar
But I don't know how can I set backBarButton as text. Should I create xib that will replace my navigation bar or I can do it programmatically

In viewDidLoad set your custom button navBar:
let button = UIButton(type: .custom)
//Set the image
button.setImage(UIImage(systemName: "chevron.backward"), for: .normal)
//Set the title
button.setTitle("Yourtitle", for: .normal)
//Add target
button.addTarget(self, action: #selector(callMethod), for: .touchUpInside) //call button tap action
// Add insets padding
let spacing: CGFloat = -10 // the amount of spacing to appear between image and title
button.imageEdgeInsets = UIEdgeInsets(top: 0, left: spacing, bottom: 0, right: 0)
button.titleEdgeInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0) // customize your button titleEdgeInsets if you want
//Create bar button
let barButton = UIBarButtonItem(customView: button)
navigationItem.leftBarButtonItem = barButton
After that create your function for button tap:
#objc fileprivate func callMethod() {
print("Your code here")
}

Related

Swift: UIBarButtonItem with bigger size than UIToolbar

I have a UIToolbar installed on my Viewcontroller on the bottom via Storyboard. I also added a bottom in the Storyboard and now I want to give this bottom a greater height than the toolbar itself.
It should be something like that, but it cannot be a Tabbar but needs to be a Toolbar, as the items on it are purely contextual actions and not top level navigation items (see Apple guidelines here and here):
I tried the following code in my Viewcontroller without success (as mentioned here):
class MyVC: UIViewController {
#IBOutlet var ibOutletForButton: UIBarButtonItem!
override func viewDidLoad() {
super.viewDidLoad()
let menuBtn = UIButton(type: .custom)
menuBtn.frame = CGRect(x: 0.0, y: 0.0, width: 20, height: 120)
menuBtn.setImage(UIImage(named:"iconImage"), for: .normal)
menuBtn.addTarget(self, action: #selector(onMenuButtonPressed(_:)), for: UIControlEvents.touchUpInside)
let menuBarItem = UIBarButtonItem(customView: menuBtn)
let currWidth = menuBarItem.customView?.widthAnchor.constraint(equalToConstant: 24)
currWidth?.isActive = true
let currHeight = menuBarItem.customView?.heightAnchor.constraint(equalToConstant: 124)
currHeight?.isActive = true
ibOutletForButton = menuBarItem
}
}
How could I get the button bigger and moved up that it looks like on the image?
One way you could do this is to add the button directly to the UIViewController instead of to the UIToolbar. You have then complete freedom of positioning and sizing.
As you don't use a UITabBar, you will stay within your UIViewController and it should be no problem
You can create 4 BarbuttonItem after first 2, give some flexible space between items and add your 'plus' button to toolbar directly in that space.
#IBOutlet weak var myToolBar: UIToolbar!
let menuBtn = UIButton(type: .custom)
override func viewDidLoad() {
super.viewDidLoad()
let menuBtn = UIButton(type: .custom)
menuBtn.frame = CGRect(x: myToolBar.center.x-10, y: -60, width: 20, height: 120)
menuBtn.setImage(UIImage(named:"iconImage"), for: .normal)
menuBtn.addTarget(self, action: #selector(onMenuButtonPressed(_:)), for: UIControlEvents.touchUpInside)
let spacer = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
let items = myToolBar.items!
myToolBar.setItems([items[0],items[1],spacer,items[2],items[3]], animated: false)
myToolBar.addSubview(menuBtn)
}

How to hide /show multiple right button in the Navigation Bar swift 5

I need to hide the two right button in the Navigation Bar, then unhide it after the user selects some options.I added two buttons(i.e search and setting button) in the storyboard
Unfortunately, the following doesn't work:
navigationItem.rightBarButtonItem.isHidden = true
if I use below code then not able to show them again
navigationItem.rightBarButtonItem = nil
Add them programatically
class YourVC: UIViewController {
var item1 = UIBarButtonItem()
var item2 = UIBarButtonItem()
override func viewDidLoad() {
super.viewDidLoad()
let btn1 = UIButton()
btn1.setImage(UIImage(named: "validImage1"), for: .normal)
btn1.frame = CGRect(x: 0, y: 0, width: 30, height: 30)
item1.customView = btn1
let btn2 = UIButton()
btn2.setImage(UIImage(named: "validImage2"), for: .normal)
btn2.frame = CGRect(x: 0, y: 0, width: 30, height: 30)
item2.customView = btn2
self.navigationItem.rightBarButtonItems = [item1,item2]
}
}
You can hide them:
self.navigationItem.rightBarButtonItems = nil
and show:
self.navigationItem.rightBarButtonItems = [item1,item2]

Swift - stop truncating UIButton titleLabel

I have the following UIButton defined:
internal lazy var volumeUnitSelector: DropDownButton = {
let button = DropDownButton()
button.setTitle("impg", for: .normal)
button.setTitleColor(UIColor.darkGreyWithDarkMode.withAlphaComponent(0.7), for: .normal)
button.titleLabel?.font = .systemFont(ofSize: 14)
button.titleLabel?.textAlignment = .right
if #available(iOS 13.0, *) {
let config = UIImage.SymbolConfiguration(pointSize: 8, weight: .black)
let symbol = UIImage(systemName: "arrowtriangle.down.fill", withConfiguration: config)?.withTintColor(UIColor.darkGreyWithDarkMode, renderingMode: .alwaysOriginal)
button.setImage(symbol, for: .normal)
}
return button
}()
And here is the DropDownButton class which subclasses UIButton:
class DropDownButton: UIButton {
override func layoutSubviews() {
self.titleEdgeInsets = UIEdgeInsets(top: 0.0, left: 0.0, bottom: 0.0, right: 9)
super.layoutSubviews()
if imageView != nil {
imageView?.translatesAutoresizingMaskIntoConstraints = false
imageView?.rightAnchor.constraint(equalTo: rightAnchor).isActive = true
imageView?.centerYAnchor.constraint(equalTo: centerYAnchor).isActive = true
self.widthAnchor.constraint(equalToConstant: 43).isActive = true
}
}
}
Here's the challenge - I'm trying to set this button to have the text 'impg' and then an arrow to indicate a drop down menu to the right of the text. So as you can see, I've added the arrow as an SFSymbol and pinned it to the right of the button. I then need me text to be essentially pinned to the leadingAnchor of the arrow image, with a small margin (0.5 / 1 point).
So, I've set the titleEdgeInset to 9 on the right hand side to make room for the image. Now this is kind of working, but what I don't understand is that my title is being clipped, when there should be plenty of room for it.
Here is the current effect it has:
And here is the xCode layout view:
I don't understand why there's not enough space here to the left of the text to expand into. I have no other constraints set.

Navigation bar back button image align with screen edge

Does anyone know how I can move a back button image to the edge of the screen?
This is what it looks like right now:
and this is how I want it to look:
If you add custom view as a button and add as leftBarButtonItem it always takes default x (frame's start position) position for that view as defined for navigation bar item. You can add something like this to get your desired output:
class AnotherViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
addBackButton()
}
func addBackButton() {
let containerView = UIView(frame: CGRect(origin: .zero, size: CGSize(width: 200, height: 50)))
let btnBack = UIButton(frame: CGRect(x: -25, y: 0, width: 45, height: 45))
btnBack.setImage(UIImage(named: "dark_ic_back.png")?.withRenderingMode(.alwaysTemplate), for: .normal)
btnBack.tintColor = .black
btnBack.addTarget(self, action: #selector(self.backAction(_:)), for: .touchUpInside)
containerView.addSubview(btnBack)
self.navigationItem.leftBarButtonItem = UIBarButtonItem(customView: containerView)
}
#objc func backAction(_ sender: UIButton) {
self.navigationController?.popViewController(animated: true)
}
}
Output:
Try to change its insets:
self.navigationController?.navigationBar.backItem?.backBarButtonItem?.imageInsets = UIEdgeInsets(top: 0, left: -12, bottom: 0, right: 12)
and find values to move it right to the edge.

Set constraint of bar button item Swift?

I'm programmatically adding a bar button item like this:
let button = UIButton.init(type: .custom)
button.setImage(UIImage(named: "Bar Chart Filled-50"), for: UIControlState.normal)
button.addTarget(self, action: #selector(self.statButtonPressed), for: UIControlEvents.touchUpInside)
button.frame = CGRect(x: 0, y: 0, width: 40, height: 40)
let barButton = UIBarButtonItem(customView: button)
navItem.rightBarButtonItem = barButton
I want it to be vertically centered in the page's navigation bar. How would I do such a thing programatically.
image: