Before I upgraded to Swift 4.2 - Xcode 10.1 the DLRadioButton I used had an even spacing between the icon and the title. I never set the spacing and everything worked fine. After the upgrade the icon and the title overlaps
The cocoapod for it says that it uses a default marginWidth of kdefaultmarginwidth
I tried to set the marginWidth in code to anything that would definitely add spacing like 50.0 but the overlap stays. I read somewhere that the kdefaultmarginwidth spacing is 5.0
How can I fix the spacing?
code:
let saleButton: DLRadioButton = {
let button = DLRadioButton(type: .custom)
button.translatesAutoresizingMaskIntoConstraints = false
button.setTitle("Sale", for: .normal)
button.setTitleColor(UIColor.lightGray, for: .normal)
button.marginWidth = 50.0 // I tried 5.0, 10.0, 20.0, even 100.0 but nothing
return button
}()
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(saleButton)
// constraints get set
}
This is a choppy fix but it works for now because everything else I tried didn't work. Inside the saleButton closure, I had to add 4 empty spaces before I set the string for the title:
I changed this:
button.setTitle("Sale", for: .normal)
to this and the overlapping is now gone
// there are 4 empty spaces in front of the word Sale
button.setTitle(" Sale", for: .normal)
Here is the image below:
Related
Button title and image not geting rendered. Works fine in iOS13 and below, but after updating to Xcode12.0.1 and iOS14, it is not working.
let viewAllButton: RoundedButton = {
let button = RoundedButton()
button.translatesAutoresizingMaskIntoConstraints = false
button.backgroundColor = .red
button.setTitle("View all", for: .normal)
button.setTitleColor(.white, for: .normal)
return button
}()
class RoundedButton: UIButton {
override func draw(_ rect: CGRect) {
super.draw(rect)
self.layer.cornerRadius = self.bounds.height / 2
self.layer.masksToBounds = true
}
}
Try to set the title color before setting setTitle. (I had some issues with something like this too).
Xcode 12.0.1, iOS 14, Swift 5.3
Actually the problem was in the redundant code which was lying around in my codebase.
extension UIButton {
open override func layoutSubviews() {
super.layoutSubviews()
}
}
I just had this sitting in my extensions. This didnt cause any issues in iOS 13 and below.
Steps to reproduce:
Add above code in your project.
Run the project(Release or Debug Mode) the project in your simulator(Button renders properly) and Kill the app.
Now, Launch the app by clicking App icon and you will see Button contents disappear.
Strange bug, i hope it save someones time.
I'm trying to realize tik tak toe game. So, I have 9 buttons and every time I'm pressing on them they install their text label as "X" or "O"
sender.setTitle("X", for: .normal)
// or
sender.setTitle("O", for: .normal)
But then , when game is finished , I want to delete all text labels and facing a problem - I can't remove text labels. I've tried several variants and still can't understand problem. I tried :
button.setTitle(nil, for: .normal)
button.setTitle("", for: .normal)
button.titleLabel?.text = ""
button.titleLabel?.text = nil
It's not working. Even if I don't see text at this buttons after my "failed reset", text is still set.
Even when I'm doing all variants to delete text and then calling
button.titleLabel?.text
Im getting not empty line or nil, Im getting "X"!!! (if there was "x" text before)
i checked it ... these all lines work .. problem is with your connection
#IBOutlet weak var button: UIButton!
override func viewDidLoad() {
DispatchQueue.main.asyncAfter(deadline: .now() + 3) {
self.button.setTitle("", for: .normal)
//self.button.backgroundColor = .red
}
super.viewDidLoad()
// Do any additional setup after loading the view.
}
I am using pushViewController like the following code:
let vc = A()
self.navigationController?.pushViewController(vc, animated:true)
I want to add a navigationItem to the page that opens. This code working on new versions but not working on iPhone 5(iOS 9.3) Simulator and iPad(10.3.3)
class A: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
configureNavigationItem()
}
func configureNavigationItem() {
let buttonLogo = UIButton(type: .custom)
buttonLogo.setImage(UIImage(named: "logo"), for: .normal)
buttonLogo.setTitle("", for: .normal)
buttonLogo.imageEdgeInsets = UIEdgeInsets(top: 0, left: 0, bottom: -9, right: 0)
let itemLogo = UIBarButtonItem(customView: buttonLogo)
self.navigationItem.setLeftBarButton(itemLogo, animated: true)
}
}
Nothing appears in old versions. How can I solve this?
The problem is that your UIButton has zero size. You need to give it a size!
buttonLogo.sizeToFit()
let itemLogo = UIBarButtonItem(customView: buttonLogo)
The reason your code works in iOS 11 is that it uses autolayout to size the button as a customView. But that is a new feature of iOS 11.
Add image with Rendering Mode something like below.
let menuButtonImage = UIImage(named: "logo")?.withRenderingMode(UIImageRenderingMode.alwaysOriginal)
buttonLogo.setImage(menuButtonImage, for: .normal)
And then check
In short, how can I change the color of the black button items (while maintaining control over their size)?
Longer version: I am programmatically adding a number of custom UIBarButtonItems to a UIToolbar. I'm using the solution found here so that I can control the size of the items.
While this fixes the UIBarButtonItem size issue, it does not respect tintColor like a typical UIBarButtonItem would. So I get something like this:
The large white item is the color I want, but not the size. This item was simply added in IB with the tintColor set to default.
The small black items are the size I want, and were added with the following code (N.B. the lines marked with // Not producing intented result):
for e in (self.profile?.expressions)! {
let button = UIButton()
button.setImage(UIImage(named: "emojiph"), for: .normal)
button.addTarget(self, action: #selector(onEmojiInsert), for: .touchUpInside)
let barItem = UIBarButtonItem(customView: button)
barItem.tag = e.family_expression_id
let wConstraint = barItem.customView?.widthAnchor.constraint(equalToConstant: 32)
wConstraint?.isActive = true
let hConstraint = barItem.customView?.heightAnchor.constraint(equalToConstant: 32)
hConstraint?.isActive = true
// Not producing intented result
button.tintColor = UIColor.white
// Not producing intented result
barItem.customView?.tintColor = UIColor.white
// Not producing intented result
barItem.tintColor = UIColor.white
self.emojibar.items?.append(barItem)
// Add flexible spacers
if (e.family_expression_id < (self.profile?.expressions.count)!) {
self.emojibar.items?.append(UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.flexibleSpace, target: nil, action: nil))
}
}
A workaround would be to provide the white image assets, but I'd prefer to find a more elegant solution if it exists.
To make the button change the color of the presented image to match the tint color then you need to initiate the button as a system type
let button = UIButton(type: .system)
In our app we are showing a burger item in the UINavigationBar.
We are using the leftBarButtonItem as the place to show it.
Here is the code to create the burger button.
let barItem = UIBarButtonItem(image: UIImage(named: "IconBurger"), style: .plain, target: target, action: selector)
barItem.tintColor = .tintColor
barItem.adjustAccessibility()
Which leads to following result on iOS 11.4 and iOS 11.3.1
iOS11.4
iOS10.3.1
As you can see the burger button somehow shrinked on iOS11.4
I fixed this by creating a custom button view like this:
let button = UIButton(type: .custom)
button.setImage(UIImage(named: "IconBurger"), for: .normal)
button.addTarget(target, action: selector, for: .touchUpInside)
let barItem = UIBarButtonItem(customView: button)
barItem.tintColor = .tintColor
barItem.adjustAccessibility()
Using this version the burger button is looking good on iOS 11.4 again.
But now when going back to 10.3.1 I was shocked because the burger button was not rendered at all anymore.
(Imagine completely black image here)
I ended up writing ugly stuff like
if #available(iOS 11.4, *) {
// show new version
} else {
// show old version
}
But I hope that can't be it!
Does anybody experienced similar or can give advise ?
Additional information: We are using pdf assets for creating UIImages in our project.
Ok. Colleague of mine found the solution which I want to document here.
Turns out calling sizeToFit() was missing on < iOS11
let button = UIButton(type: .custom)
let image = UIImage(named: "IconBurger")
button.setImage(image, for: .normal)
button.addTarget(target, action: selector, for: .touchUpInside)
button.sizeToFit()
let item = UIBarButtonItem(customView: button)
item.adjustAccessibility()
return item