Change UI based on System Language - Swift - ios

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.

Related

Swift - SwipingController Programmatic - button doesn't work

I created the SwipingController app.
The application was supposed to have the functionality of scrolling with gestures and a management bar with 2 buttons and UIPageControl.
For now, these buttons were supposed to print only a text message in the console, but it doesn't.
let nextButton: UIButton = {
let button = UIButton(type: .system)
button.setTitle("NEXT", for: .normal)
button.setTitleColor(.black, for: .normal)
button.titleLabel?.font = UIFont.boldSystemFont(ofSize: 14)
button.addTarget(self, action: #selector(handleNextButton), for: .touchUpInside)
return button
}()
#objc func handleNextButton() {
print("Next Botton Pressed")
}
I wanted to add the whole page management bar in a separate file.
When it goes to the main controller, the whole functionality work.
I don't want to paste all the code, so it gives a link to the git
https://github.com/SebaKrk/SwipingControllerProgrammatic.git
Picture from simulator
The Problem is that you set your target right in the setup code of your UIButton
let previousBotton : UIButton = {
let button = UIButton(type: .system)
button.setTitle("PREV", for: .normal)
button.setTitleColor(.black, for: .normal)
button.titleLabel?.font = UIFont.boldSystemFont(ofSize: 14)
button.addTarget(self, action: #selector(handlePreviousButton), for: .touchUpInside)
return button
}()
It seems like the self is not initialized at this point. Because this code is run before your init was run.
So you have to set the target of the Button after you called super.init then it works.

UIButton Title Label Text Not Changing Using Storyboard or Programmatically

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
}

Stripe / Apple-pay Icon presented wrongly/ rejected by apple store

Apple rejected my app because Stripe pod show inappropriate apple pay icon, which added an extra "apple pay" next to the apple pay icon. (see attached)
Is anyone had the same problem like me and what are your solution?
Is anyone had successfully uploaded an app with apple pay function installed?
Please See my calling code as below too, many thank!
private func reloadPaymentButtonContent() {
guard let selectedPaymentMethod = paymentContext.selectedPaymentOption else {
// Show default image, text, and color
paymentButton.setImage(#imageLiteral(resourceName: "Payment"), for: .normal)
paymentButton.setTitle("付款", for: .normal)
paymentButton.setTitleColor(.gray, for: .normal)
return
}
// Show selected payment method image, label, and darker color
paymentButton.setImage(selectedPaymentMethod.image, for: .normal)
paymentButton.setTitle(selectedPaymentMethod.label, for: .normal)
paymentButton.setTitleColor(.blue, for: .normal)
}
private func reloadBuyButton(){
if paymentContext.selectedPaymentOption == nil {
// buySubscriptionButton.backgroundColor = .loopGrayColor
// buySubscriptionButton.setTitle("订阅", for: .normal)
// buySubscriptionButton.setTitleColor(.white, for: .normal)
buySubscriptionButton.isEnabled = false
}else{
buySubscriptionButton.isEnabled = true
// buySubscriptionButton.backgroundColor = .loopGreenColor
// buySubscriptionButton.setTitle("订阅", for: .normal)
// buySubscriptionButton.setTitleColor(.white, for: .normal)
}
You're setting both in the code. So, either set Image or Label text.

Remove button when click itselt in swift 3

Hello I have a custom UIButton has added. And I want to remove this button when click on it self. I have done like this.
btnDelete.addTarget(self, action: #selector(deleteCoveringPerson(sender:)), for: .touchUpInside)
btnDelete.setImage(UIImage.init(named: "close-dark"), for: .normal)
btnCoveringPerson.addSubview(btnDelete)
And this is my delete button selector
func deleteCoveringPerson(sender:UIButton)
{
dm.strCoveringPersonNAme=""
dm.strcoveringPersonCode="0"
btnCoveringPerson.setTitle(lan.getConvertedLanguageString(word: "COVERINGPERSON"), for: .normal)
btnDelete.removeFromSuperview()
}
How can I do this?
For me this work just fine
func deleteCoveringPerson(sender:UIButton)
{
dm.strCoveringPersonNAme=""
dm.strcoveringPersonCode="0"
btnCoveringPerson.setTitle(lan.getConvertedLanguageString(word: "COVERINGPERSON"), for: .normal)
sender.removeFromSuperview()
}
Hope this helps

Can't change UIbutton image for different state

I want to change the image of a UIButton for different states. To achieve this, I'm using:
btn.setImage(UIImage(named: "blabla"), for .normal)
and
btn.setImage(UIImage(named: blabla2), for .disabled)
This only makes some appear dimmed.
What did I do wrong? I just want to make my button appearance the same for different states, how?
(my button type - .system).
This helped me (swift 3.0)
btn.setImage(UIImage(named:"yourFriend")?.withRenderingMode(.alwaysOriginal), for: .normal)
btn.setImage(UIImage(named:"yourFriend")?.withRenderingMode(.alwaysOriginal), for: .disabled)
You just need to set one for the state. And if you don't set another image for different state. It would look the same in all state.
button.setImage(image, forState: .Normal)
How to change UIButton image in Swift
For display disabled button set image
let btn = UIButton(type: .Custom)
btn.setImage(UIImage(named: blabla2), for .disabled)
Then
btn.enabled = false // to display Disable image
btn.enabled = true // to display Normal image
private let button1: UIButton = {
let button = UIButton(type: .custom)
button.setImage(UIImage(named:"firstButtonNormalStateImage"), for: .normal)
button.setImagesetImage(UIImage(named:"firstButtonSelectedStateImage"), for: .selected)
return button
}()
private let button2: UIButton = {
let button = UIButton(type: .custom)
button.setImage(UIImage(named:"secondButtonNormalStateImage"), for: .normal)
button.setImage(UIImage(named:"secondButtonSelectedStateImage"), for: .selected)
return button
}()
// implement for example in viewDidLoad()
button1.addTarget(self, action: #selector(firstButtonDidTap), for: .touchUpInside)
button2.addTarget(self, action: #selector(secondButtonDidTap), for: .touchUpInside)
// trigger actions
#objc func firstButtonDidTap() {
button1.isSelected = true
button2.isSelected = false
}
#objc func secondButtonDidTap() {
button2.isSelected = true
button1.isSelected = false
}
For whoever is still having this issue (currently Xcode 10.0) with a Custom button, I found I was able to change the text and/or image if instead of:
myButton.setTitle("Hi", for: [.normal])
I used this:
myButton.setTitle("Hi", for: []) //remove specific states
I don't know why .normal was not working for me, even though the button was definitely enabled. But maybe this will save someone else a headache!
You can simply do this by StoryBoard as well.
Select the button, got to identity inspector and do the following:-
Firstly set the buttonType to custom instead of system.
Secondly choose state Config to lets say default and give the imageName in "image" attribute, similarly choose other state configs (Highlighted, disabled, selected etc.) and set images as required by you.
Then later in the code you just have to control and set the state of the button, and respective image will be shown to you.

Resources