how to remove padding left UIBarButtonItem? - ios

I would like to use a custom View as UIBarButtonItem left in an UINavigationController, is it possible to remove the left padding here?
Ill alrady tried:
let btn = UIButton()
btn.frame = CGRect(x: -50, y: -50, width: 44, height: 50)
btn.setImage(UIImage(name:"img").withRenderingMode(.alwaysTemplate), for: .normal)
btn.addTarget(self, action: #selector(openSetting), for: .touchUpInside)
let leftButtonBar = UIBarButtonItem(customView: btn)
self.navigationItem.leftBarButtonItems = ( [leftButtonBar , otherBtn ])
how to remove space left setting icon
update. tired this code:
let view = UIView(frame: CGRect(x: 0, y: 0, width: 50, height:
view.backgroundColor = .gray
let btn = UIButton()
btn.frame = CGRect(x: -15, y: 0, width: 44, height: 50)
btn.setImage(UIImage(name:"img").withRenderingMode(.alwaysTemplate), for: .normal)
btn.addTarget(self, action: #selector(DashboardTabBarController.openSetting), for: .touchUpInside)
view.addSubview(btn)
let leftButtonBar = UIBarButtonItem(customView: view)
problem this user when clicked btn setting user
and other problem title navigation not center align

You should have noticed that whatever x value you provide to your custom view i.e, btn, it will always start from the same position because internally navigation bar doesn't count for position provided for that item and it always set the internally defined position as in below image, I set the background color to the custom button for better visuals.
So, the trick here is to put your custom button into another container view as below,
let view = UIView(frame: CGRect(origin: .zero, size: CGSize(width: 200, height: 50)))
view.backgroundColor = .green
let btn = UIButton(frame: CGRect(x: -10, y: 10 , width: 30, height: 30))
btn.setImage(UIImage(named: "cancelled")?.withRenderingMode(.alwaysTemplate), for: .normal)
btn.addTarget(self, action: #selector(openSetting), for: .touchUpInside)
btn.backgroundColor = .yellow
btn.tintColor = .red
view.addSubview(btn)
let btn2 = UIButton(frame: CGRect(x: 20, y: 10, width: 30, height: 30))
btn2.setImage(UIImage(named: "cancelled")?.withRenderingMode(.alwaysTemplate), for: .normal)
btn2.contentMode = .scaleAspectFit
btn2.tintColor = .yellow
btn2.addTarget(self, action: #selector(openSetting), for: .touchUpInside)
view.addSubview(btn2)
let itemsContainer = UIBarButtonItem(customView: view)
self.navigationItem.leftBarButtonItems = [itemsContainer]
This will result into this,
Now, if you change the x, y, width, height of btn, it will adjust accordingly.

Please try this. It works fine for me. If this code not working properly then I will give you another solution.
let view = UIView(frame: CGRect(x: 0, y: 0, width: 50, height: 50))
let btn = UIButton()
btn.frame = CGRect(x: -15, y: 0, width: 44, height: 50)
btn.setImage(UIImage(name:"img").withRenderingMode(.alwaysTemplate), for: .normal)
btn.addTarget(self, action: #selector(openSetting), for: .touchUpInside)
view.addSubview(btn)
let leftButtonBar = UIBarButtonItem(customView: view)
self.navigationItem.leftBarButtonItems = ( [leftButtonBar])
Please add other button in View and set the width of view accordingy.It may hepls to you.Thank you

You can set imageInsets and set image this is example of left margin to set back image on position of default back button
let rightBtn = UIBarButtonItem(image: UIImage(named: "BackArrow"), style: .done, target: self, action: #selector(actnClose))
rightBtn.imageInsets = UIEdgeInsets(top: 0, left: -12, bottom: 0, right: 0)
self.navigationItem.setLeftBarButton(rightBtn, animated: true)

Set the x value of cRect to zero:
btn.frame = CGRect(x: 0, y: -50, width: 44, height: 50)

The less painful and actually cleanest solution was to hide the left button and just add a subview directly to the navigationBar and customise it's position and size with the good old constraints. Worked like a charm. Haven't tested it in every situation but seems to work fine.

Related

UIButton Text is cut off not according to the width, how to set width wrap content?

these several days im learning iOS Development, I have a problem where my text button is clipped to the wrong width,
how can I make the UIButton match the width of the content? below is my code
let btnPost = UIButton()
btnPost.setTitle("POST", for: .normal)
btnPost.backgroundColor = Color.shared.accent
btnPost.titleLabel?.font = UIFont.boldSystemFont(ofSize: 14)
btnPost.layer.cornerRadius = 10
btnPost.layer.masksToBounds = true
btnPost.titleEdgeInsets = UIEdgeInsets(top: 5, left: 15, bottom: 5, right: 15)
btnPost.addTarget(
self,
action: #selector(baseDidTapPost(_:)),
for: .touchUpInside)
let barButtonItem = UIBarButtonItem(customView: btnPost)
navigationItem.rightBarButtonItem = barButtonItem
Thanks
You have to just remove the btnPost.titleEdgeInsets and add a frame to your button
btnPost = UIButton(frame: CGRect(x: 0, y: 0, width: 80, height: 20))
complete code :
let btnPost = UIButton(frame: CGRect(x: 0, y: 0, width: 80, height: 20))
btnPost.setTitle("POST", for: .normal)
btnPost.backgroundColor = UIColor.blue
btnPost.titleLabel?.font = UIFont.boldSystemFont(ofSize: 14)
btnPost.layer.cornerRadius = 10
btnPost.layer.masksToBounds = true
btnPost.addTarget(self,action: #selector(baseDidTapPost(_:)),for: .touchUpInside)
let barButtonItem = UIBarButtonItem(customView: btnPost)
navigationItem.rightBarButtonItem = barButtonItem

button image is not show in rightView of UItextfield

I have created a button image and placed it on the rightView of a UITextField "password" using Sa wift. I want to create toggle button hide/show secure text in the password field. The image shown on the right view.
Code:
func passwordToggleButton(){
let button = UIButton(type: .custom)
button.setImage(UIImage(named: "hidePassword"), for: .normal)
button.imageEdgeInsets = UIEdgeInsets(top: 0, left: -16, bottom: 0, right: 0)
button.frame = CGRect(x: CGFloat(txtfPassword.frame.size.width - 25), y: CGFloat(5), width: CGFloat(25), height: CGFloat(25))
button.backgroundColor = UIColor.white
button.addTarget(self, action: #selector(self.togglePassword), for: .touchUpInside)
txtfPassword.rightView = button
txtfPassword.rightViewMode = .always
}
image
Since you are putting it inside “rightView”.
The X and Y coordinates of the frame become relative to it.
So you should change them to 0, 0

How can I center my navigationItem.titleView when I add more buttons to the navigationItem?

I'm doing a tutorial that replicates Twitter's iOS app.
I have a BarButtonItem on the left, an ImageView in the middle, and an array of two BarButtonItems on the right. If the array is only populated with one button, the titleView is centered. If I add the other button, the titleView moves over.
I understand the issue lies with the width of the titleView as shown in the pictures at the end.
let titleImageView = UIImageView(image: #imageLiteral(resourceName: "title_icon"))
titleImageView.frame = CGRect(x: 0, y: 0, width: 24, height: 24)
titleImageView.contentMode = .scaleAspectFit
navigationItem.titleView = titleImageView
let followButton = UIButton(type: .system)
followButton.setImage(#imageLiteral(resourceName: "follow").withRenderingMode(.alwaysOriginal), for: .normal)
followButton.frame = CGRect(x: 0, y: 0, width: 24, height: 24)
navigationItem.leftBarButtonItem = UIBarButtonItem(customView: followButton)
let searchButton = UIButton(type: .system)
searchButton.setImage(#imageLiteral(resourceName: "search").withRenderingMode(.alwaysOriginal), for: .normal)
searchButton.frame = CGRect(x: 0, y: 0, width: 24, height: 24)
let composeButton = UIButton(type: .system)
composeButton.setImage(#imageLiteral(resourceName: "compose").withRenderingMode(.alwaysOriginal), for: .normal)
composeButton.frame = CGRect(x: 0, y: 0, width: 24, height: 24)
// THIS IS THE PROBLEM
navigationItem.rightBarButtonItems = [UIBarButtonItem(customView: composeButton), UIBarButtonItem(customView: searchButton)]
navigationItem.rightBarButtonItems = [UIBarButtonItem(customView: composeButton)]:
navigationItem.rightBarButtonItems = [UIBarButtonItem(customView: composeButton), UIBarButtonItem(customView: searchButton)]:
Thank you!
You can insert a dummy button on the left side of the navigationbar just for balance. Make the button the same size as the searchButton and clear background.

How to increase the UIButton Image size in Swift3?

I am using the default UIImage property of the UIButton to set the back image . I have successfully done it but it seems that the image is appearing small. How to increase the size of the image in UIButton?
let viewForNavigation = UIView(frame: CGRect(x: 0, y: 0, width: 320, height: 40))
viewForNavigation.backgroundColor = UIColor.clear
let backbutton = UIButton(frame: CGRect(x: 0, y: 8, width: 320, height: 20))
backbutton.setImage(UIImage(named: "back_button"), for: UIControlState.normal)
backbutton.imageEdgeInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right:10)
backbutton.contentHorizontalAlignment = .left
backbutton.titleEdgeInsets = UIEdgeInsets(top: 0, left: 10, bottom: 0, right: 0)
backbutton.titleLabel!.font = UIFont().robotoMedium(withFontSize: 20)
backbutton.setTitle(title, for: .normal)
backbutton.setTitleColor(UIColor.black, for: UIControlState.normal)
backbutton.addTarget(self, action: #selector(exitViewController), for: UIControlEvents.touchUpInside)
viewForNavigation.addSubview(backbutton)
let rightBarButton = UIBarButtonItem(customView: viewForNavigation)
self.navigationItem.leftBarButtonItem = rightBarButton
I think you have choose wrong size back button image, please use below image for back button:
Output

Bound the button to top right corner of the view iOS swift

I have a view in which i have multiple labels and a button i want the button to be bound to the top right corner
when i apply the add Missing constraints layout the button gets separate position for different devices Please help me to resolve this
NEVER use add missing constraints. This is a bad way of constructing your layout.
The easiest way to get your desired look is to decide on the size you want your button to be (eg. 30x30)
Then you add the following constraints to your button:
This sets the button's width & Height:
Then pin it to the right top corner:
//Just in case if anybody needs it with code:
//I have added 4 buttons to a custom view's each corner:
var customView = UIView()
override func viewDidLoad() {
super.viewDidLoad()
customView.frame = CGRect.init(x: 0, y: 0, width: 200, height: 200)
customView.backgroundColor = UIColor.red //give color to the view
customView.center = self.view.center
let crossButton = UIButton(frame: CGRect(x: -10, y: -10, width: 30, height: 30))
crossButton.layer.cornerRadius = 15
crossButton.backgroundColor = UIColor.black
crossButton.setImage(UIImage(named: "cross.png"), for: .normal)
crossButton.addTarget(self, action: #selector(crossButtonTapped), for: .touchUpInside)
crossButton.autoresizingMask = [.flexibleLeftMargin, .flexibleBottomMargin]
let flipButton = UIButton(frame: CGRect(x: customView.frame.width-15, y: -10, width: 30, height: 30))
flipButton.layer.cornerRadius = 15
flipButton.backgroundColor = UIColor.black
flipButton.setImage(UIImage(named: "done.png"), for: .normal)
flipButton.addTarget(self, action: #selector(crossButtonTapped), for: .touchUpInside)
flipButton.autoresizingMask = [.flexibleLeftMargin, .flexibleBottomMargin]
let scaleButton = UIButton(frame: CGRect(x: -10, y: customView.frame.height-20, width: 30, height: 30))
scaleButton.layer.cornerRadius = 15
scaleButton.backgroundColor = UIColor.black
scaleButton.setImage(UIImage(named: "done.png"), for: .normal)
scaleButton.addTarget(self, action: #selector(crossButtonTapped), for: .touchUpInside)
scaleButton.autoresizingMask = [.flexibleLeftMargin, .flexibleBottomMargin]
let editButton = UIButton(frame: CGRect(x: customView.frame.width-20, y: customView.frame.height-20, width: 30, height: 30))
editButton.layer.cornerRadius = 15
editButton.backgroundColor = UIColor.black
editButton.setImage(UIImage(named: "done.png"), for: .normal)
editButton.addTarget(self, action: #selector(crossButtonTapped), for: .touchUpInside)
editButton.autoresizingMask = [.flexibleLeftMargin, .flexibleBottomMargin]
customView.addSubview(crossButton)
customView.addSubview(flipButton)
customView.addSubview(scaleButton)
customView.addSubview(editButton)
self.view.addSubview(customView)
}
func crossButtonTapped(_ sender:UIButton) {
print("Cross Button was tapped")
}

Resources