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

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.

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

UIBarButtonItem is too wide [duplicate]

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.

Navigation Left bar button item with back button [duplicate]

This question already has answers here:
How do I add a left bar button without overriding the natural back button?
(2 answers)
Closed 6 years ago.
I want to add left bar button item in navaigationBar.
I have back button sign (do not have any text) in left side and when I add leftBarButtonItem then I can not see the back button.
How can I solve this problem.
This is my code:
let phonePhoto = UIButton()
phonePhoto.setImage(UIImage(named: "navigationPhone"), for: UIControlState())
phonePhoto.frame = CGRect(x: 0, y: 0, width: 40, height: 40)
phonePhoto.addTarget(self, action: #selector(userEdit), for: .touchUpInside)
let userPhone = UIBarButtonItem()
userPhone.customView = phonePhoto
self.navigationItem.leftBarButtonItem = userPhone
The presence of custom left bar button items causes the back button to be removed in favor of the custom items. Set navigationItem property leftItemsSupplementBackButton to true will display back button also with your custom left bar item.
self.navigationItem.leftItemsSupplementBackButton = true
Check Apple Documentation on leftItemsSupplementBackButton for more details.

Remove UINavigationBar border [duplicate]

This question already has answers here:
How to hide UINavigationBar 1px bottom line
(49 answers)
Closed 7 years ago.
There is a thin gray border in the bottom of UINavigatioBar:
I can't remove it!
I've tried:
self.navigationController?.navigationBar.layer.borderColor =
UIColor.orangeColor().CGColor
self.navigationController?.navigationBar.layer.borderWidth = 0
With no chance.
It should work
self.navigationController?.navigationBar.setBackgroundImage(UIImage(), forBarMetrics: UIBarMetrics.Default)
self.navigationController?.navigationBar.shadowImage = UIImage()
Add this line for background color (Change rgb value of your choice)
self.navigationController?.navigationBar.barTintColor = UIColor(rgba: "#000000")