UIBarButtonItem is too wide [duplicate] - swift

This question already has answers here:
Change size of UIBarButtonItem (image) in Swift 3
(8 answers)
Closed 3 years ago.
I am trying to add a UIBarButtonItem to my UINavigationController.
The button is too wide though and the hit area seems to stretch beyond the bounds of the image.
In my viewDidLoad I am simply applying:
let leftNavButton = UIBarButtonItem(image: #imageLiteral(resourceName: "hamburger_icon"), style: .plain, target: nil, action: nil)
navigationItem.leftBarButtonItem = leftNavButton

There can be a problem with an image size of hamburger_icon, try to use a random system icon to see if the image is still too wide.

Related

How can I get CGRect of tapped rectangle in ScrollView? [duplicate]

This question already has answers here:
How to get local tap position in onTapGesture?
(2 answers)
Closed 11 months ago.
Hello, I want to know the CGRect(coordinates and width/height) of dynamic size rectangle which user tapped in ScrollView.
Is there any way to get this?
If you have added UITapGestureRecognizer in every view then you can get the tapped view frame as -
#objc func tapFunction(gesture: UITapGestureRecognizer) {
let viewFrame = gesture.view.frame
let height = viewFrame.height //for height
}
You need to add gesture in every view with a parameter.
let tap = UITapGestureRecognizer(target: self, action: #selector(tapFunction(gesture:)))
yourView.addGestureRecognizer(tap)

Unable to change the color of UIBarItem programmatically swift [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I am unable to change the color of UIBarItem programmatically swift. I want to change the color of this "create" button whether something happens. But nothing changes the color of the button.
Here is an image of the button :
here is how You can achieve it programatically in viewDidLoad add this
override func viewDidLoad() {
super.viewDidLoad()
let butt = createButton.customView as? UIButton
butt?.setTitleColor(.green, for: .normal)
// Do any additional setup after loading the view.
}
Please try tintColor attribute
let rightBarButtonItem = UIBarButtonItem(title: "Text", style: .plain, target: self, action: #selector(objcAction))
rightBarButtonItem.tintColor = UIColor.red
navigationItem.rightBarButtonItem = rightBarButtonItem

swift xcode - navigationitem.rightbarbuttonitem not showing image literal though just appearing blue

just showing blue and not image literal
My rightBarButtonItem is just showing blue and not the image literal?
NavigationItem.rightBarButtonItem = UIBarButtonItem(image: #imageLiteral(resourceName: "Contacts Book"), style: .plain, target: self, action: #selector(handleNewMessage))
I think it's safe to assume this isn't a png with a transparent background.
barButtonItems take pngs and fill every existing pixel with default color blue.
You can change the color, but I assume you don't want any other block in place of the blue one.
You can either find a way to make this not a bar button item or you can edit your image to take the background out.
Something like this:
Works better than something like this:
You need to set property of your image that is below:
Step1:Go to Assets.xcssets
Step2:Select your imageset
Step3:Go to property inspector
Step4:In imageset you need to set Original Image into Render as property.
it worked by doing this
NavigationItem.rightBarButtonItem = UIBarButtonItem(image: UIImage(named:"Grape")?.withRenderingMode(.alwaysOriginal), style: .plain, target: self, action: #selector(handleNewMessage))
now it shows the actual image and not just a complete blue image

Swift UiButton how to left align the title? [duplicate]

This question already has answers here:
Trouble left-aligning UIButton title (iOS/Swift)
(2 answers)
Closed 2 years ago.
I have a UIButton created programmatically but can't find a way to left align the title.
This does nothing:
button.setTitle("DESCRIPTION", for: .normal)
button.titleLabel?.textAlignment = .left
Try this one
button.contentHorizontalAlignment = .left
For left side aligment:
buttonItem.contentHorizontalAlignment = .left
For right side aligment:
buttonLogout.contentHorizontalAlignment = .right
and bydefault alignment for button title is center.

How can I set image location correctly in Swift?

In my app, I am using SWRevealViewController. Currently everything is working OK except for the menu image position.I set up the menu button programatically. Now I need to put it to the correct place.It also needs to work in all screen sizes and needs to be at the same place.
I set up the menu button like this
override func viewDidAppear(animated: Bool) {
let image = UIImage(named: "reveal-icon#2x.png")
tabBarController?.navigationItem.leftBarButtonItem = UIBarButtonItem(title: "", style:
UIBarButtonItemStyle.Plain, target: self.revealViewController(), action: Selector("revealToggle:"))
tabBarController?.navigationItem.leftBarButtonItem?.image = image
}
and here is the result
How do I fix it? I think I need to do it programatically...
And here is the overall layout of my storyboard file.
Thanks for your answers.
This might be happening because you are initializing the UIBarButtonItem with a title. Initialize it with something like this:
let img = UIImage(named: "reveal-icon#2x.png")!.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
let leftBarButtonItem = UIBarButtonItem(image: img, style: UIBarButtonItemStyle.Plain, target: self.revealViewController(), action: Selector("revealToggle:"))
tabBarController?.navigationItem.leftBarButtonItem = leftBarButtonItem