Xcode 12.0.1
I am trying to remove the title label text on a UIButton using the storyboard. I then plan on setting it programmatically. When removing the text via the storyboard, the preview shows the text removed. However, the text remains when I build and run the application.
Changing programmatically is not working either. I try to change in in the controllers viewDidLoad()
punchBtn.setTitle("Transfer", for: .normal)
punchBtn.setTitle("Transfer", for: .application)
punchBtn.setTitle("Transfer", for: .selected)
punchBtn.setTitle("Transfer", for: .reserved)
punchBtn.setTitle("Transfer", for: .highlighted)
punchBtn.setTitle("Transfer", for: .disabled)
punchBtn.setTitle("Transfer", for: .focused)
The interesting thing is the text is changing when the button is selected:
Any advice would be greatly appreciated.
In the file Main.strings you will find this word "punch".
change it from there or else take a new button.
This does sound like a cache issue, because nowhere in the app does it say "Punch" anymore.
Command-Shift-K to clean the project hopefully fixes that.
If that doesn't work, then search the entire workspace for instances where you used the word "punch".
Try this!!!
override func viewWillAppear(_ animated: Bool) {
// other code
// end of method
// Change the text of your button
}
Related
I have an app and I want to support 2 languages. English and Greek.
I have already localised the whole app and everything works perfect except the UI.
I want to change images based on the system language user has. It works fine on simulator but when I use my real device the app changes its language except the images.
This is my code for the change.
let language = NSLocale.current.identifier
override func viewDidLoad() {
super.viewDidLoad()
setupUI()
}
func setupUI() {
if language == "el_US" {
print(language)
scanBtn.setBackgroundImage(UIImage(named: "scanBtnGr"), for: .normal)
detoxBtn.setBackgroundImage(UIImage(named: "detoxGr"), for: .normal)
bioBtn.setBackgroundImage(UIImage(named: "bioBtn"), for: .normal)
searchBtn.setBackgroundImage(UIImage(named: "browseGr"), for: .normal)
captureBtn.setBackgroundImage(UIImage(named: "scanGr"), for: .normal)
uploadBtn.setBackgroundImage(UIImage(named: "uploadGr"), for: .normal)
}else{
print(language)
scanBtn.setBackgroundImage(UIImage(named: "scanBtn"), for: .normal)
detoxBtn.setBackgroundImage(UIImage(named: "detoxBtn"), for: .normal)
bioBtn.setBackgroundImage(UIImage(named: "bioBtn"), for: .normal)
searchBtn.setBackgroundImage(UIImage(named: "browseBtn"), for: .normal)
captureBtn.setBackgroundImage(UIImage(named: "scan"), for: .normal)
uploadBtn.setBackgroundImage(UIImage(named: "upload"), for: .normal)
}
}
I really don't know where is the problem because on the Simulator works perfect.
Changing the way you set those images as below can help.
scanBtn.setBackgroundImage(UIImage(named: NSLocalizedString(key: "scanBtnGr", comment: ""), for: .normal)
In this way, you won't have to worry about the target language code as well. You can remove if statement and define corresponding names in Localizable.strings file.
Besides, if you change the language of your app without terminating the app and changing the device language, please make sure that setupUI() function gets invoked one more time after the language switch. viewWillAppear may be a suitable place for it.
I am trying to make my button, when tapped, to push to a new View Controller. I've tried many different ways but it won't trigger the function that I have it linked to. I also checked the 3D stack view of my layers and the button is on top and clickable, even when I check the background color, it's not being covered by anything else.
Does anyone have any ideas to what I am doing wrong?
For now I am trying to make the button print out the sentence in the console, however whenever I press it, the string doesn't pop up, so I haven't bothered to connect it to the view controller yet.
Also, I am coding this app without storyboards.
Here is my code below.
It is under the MainPageCell class declared as a UICollectionViewCell
private let playButton: UIButton = {
let button = UIButton()
button.setTitle("", for: .normal)
button.backgroundColor = .clear
button.translatesAutoresizingMaskIntoConstraints = false
button.addTarget(self, action: #selector(MainPageCell.buttonTapped), for: .touchUpInside)
return button
}()
#objc func buttonTapped() {
print("I PRESSED THE BUTTON")
}
This line is wrong:
button.addTarget(self, action: #selector(MainPageCell.buttonTapped), for: .touchUpInside)
You cannot assign self as the action target in a property declaration initializer, because the instance designated by self does not exist yet. There is no error or warning (I regard that as a bug), but the action method is never called.
Move that assignment elsewhere and rewrite it, like this:
self.playButton.addTarget(self, action: #selector(MainPageCell.buttonTapped), for: .touchUpInside)
Maybe try defining your button action under the UIView Class, I've had a problem like that before, only worked when i linked it to the View Class, Good luck
I have a custom UIButton but I am not able to make changing background or text color based on a quick tap. If something works, it's only on long press:
buton.isUserInteractionEnabled = true
buton.setTitle("text_normal", for: .normal)
buton.setTitle("text_highlighted", for: .highlighted)
buton.setTitle("text_selected", for: .selected)
buton.setTitle("text_focused", for: .focused)
The only text I can get is "text_highlighted" after holding the button ~1 second. But nothing happens on short tap.
But the action is triggered correctly:
let tap2 = UITapGestureRecognizer(target: self, action: #selector(handleTap))
buton.addGestureRecognizer(tap2)
#objc func handleTap(sender: UITapGestureRecognizer) {
print("click")
}
What I tried:
Adding custom
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
...
which is also triggered later. And combining with tocuhesEnded didn't change the color temporarily as well (maybe it was too quick).
I can't use UIButton(type: .system) as well - it's not system styled button.
Similar as with changing text this is not working as well:
self.setTitleColor(UIColor.gray, for: .normal)
self.setTitleColor(UIColor.yellow, for: .selected)
self.setTitleColor(UIColor.yellow, for: .highlighted)
self.setTitleColor(UIColor.yellow, for: .focused)
I can see yellow only when long pressed. I am looking for something like this, but it needs to work on quick tap as well.
Custom button is modifying layoutSubviews(), but not colors. Custom button contains default image and label. Whole button has rounded corners. But overall nothing special is in there.
I am not using any storyboard or XIB - everything is in Swift 4 programatically.
The button is supposed to lead to another ViewController, but I want to see the immediate feedback on click. Something like when created from storyboard: https://youtu.be/lksW12megQg?t=3m25s - not even simple alpha change works for me right now.
check isselected property of uibutton if button is selected then change the background color of button
Jen Jose was right - there was a problem with my parent class which was 'eating up' my touches. I confirmed this when moving it to different ViewController + I had the same issue with table, which couldn't handle touch events (https://stackoverflow.com/a/9248827/1317362)
EDIT:
To be precise - this button was in a UIScrollView.
Adding scrollView.delaysContentTouches = NO; solved the issue completely.
Details: https://stackoverflow.com/a/16650610/1317362
After updating to Swift 4, I am getting a compiler error:
Static member 'appearance' cannot be used on protocol metatype 'UIAppearance.Protocol'
Here is my viewWillAppear method in my custom Tab Bar Controller subclass, I am setting the font of the item text.
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
// compiler error on line below
UIAppearance.appearance().setTitleTextAttributes([NSAttributedStringKey.font: font], for: UIControlState.normal)
}
I'm having trouble fixing this, any guidance would be appreciated, thanks!
Right - the current Swift 4 conversion tool (as of Xcode 9 Beta 4) gets a little carried away.
I was able to fix the problem quickly by reverting the UIAppearance conversion code, then updating the individual attributes.
For example, in Swift 3 I had:
UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.white], for: .selected)
Xcode "helped" me out by changing it to:
UIAppearance.appearance().setTitleTextAttributes([NSAttributedStringKey.foregroundColor: UIColor.white], for: .selected)
I was able to quiet the errors by half-reverting, to:
UITabBarItem.appearance().setTitleTextAttributes([NSAttributedStringKey.foregroundColor: UIColor.white], for: .selected)
My code so far in getting slider to work with custom background image
self.slider.setThumbImage(#imageLiteral(resourceName: "thumb"), for: .normal)
self.slider.setMinimumTrackImage(#imageLiteral(resourceName: "slider"), for: .normal)
self.slider.setMaximumTrackImage(#imageLiteral(resourceName: "slider2"), for: .normal)
self.slider.setValue(0.5, animated: true)
I want a slider like temperature where user scroll and it fill with color. But unable to do so. Please help
This one is not working anymore
Dashed slider