I am configuring my tableview cell like I mentioned below. It works, but the problem is there is unwanted space before the title 'Closed' since I am removing the image. How do I remove the unwanted space, so that my 'Closed' title will be centered?
if (condition) {
self.myButton.setImage(UIImage(named: "activeImage"), forState: .Normal)
self.myButton.setTitle("Active", forState: .Normal)
}
else {
self.myButton.setImage(nil, forState: .Normal)
self.myButton.setTitle("Closed", forState: .Normal)
}
You might be missing this in else condition
self.myButton.contentHorizontalAlignment = .Center
self.myButton.contentVerticalAlignment = .Center
Related
I'm having a hard time changing the text color for the placeholder label that is used as a hint when not in focus and as a label on top when in focus. (Photo below)
The is reason I'm switching out all my MDC-TextFields and MDC-TextInputControllers is because they are all being deprecated for the New MDC-Outlined Textfields.
The code below is a function within an extension that would simply setup the general background & sub-label colors for all MDC-Outlined Textfields throughout the app.
I have tried a number of functions with no luck(commented out below).
extension MDCOutlinedTextField {
func setUpGeneralBackgroundColors(){
//Text color
self.setTextColor(UIColor.white, for: .normal)
self.setTextColor(UIColor.white, for: .editing)
//Border color
self.setOutlineColor(UIColor.white, for: .normal)
self.setOutlineColor(UIColor.white, for: .editing)
//self.setFloatingLabelColor(UIColor.white, for: .normal)
//self.setFloatingLabelColor(UIColor.white, for: .editing)
//self.setFloatingLabelColor(UIColor.white, for: .disabled)
// self.setNormalLabelColor(UIColor.purple, for: .normal)
// self.setNormalLabelColor(UIColor.purple, for: .editing)
// self.setNormalLabelColor(UIColor.purple, for: .disabled)
// self.label.tintColor = UIColor.purple
//self.label.textColor = UIColor.systemPink
//self.label.shadowColor = UIColor.cyan
//self.label.backgroundColor = UIColor.red
//Changes icon colors within the text field if any
self.tintColor = .green
}
}
I figured the problem, in the storyboard where I have these new textfields I had a raw string for the placeholder which override the swift code behind the scenes and prevented me from changing colors.
Note: This problem will NOT occur with the now deprecated MDC-Textfields.
If you guys have a placeholder value in the storyboard when using these MDC-Outlined Textfields in storyboards, (not to mixed up with the swift code) then get rid of them.
I have a tableview cell that has an image for a like button that is set like this,
upArrow.setImage(UIImage(named: "likeButtonFill"), for: .selected)
upArrow.setImage( UIImage(named: "likeButton"), for: .normal)
however, whenever I set
upArrow.isSelected = true
the image on the button never changes.
I do my swift project using code only no storyboards, so no storyboard solutions please I have an agree to terms button and when selected the checkmark is covered in the tint color I am trying to prevent the tint color highlighting the image when selected I am not sure why this is happening here is the button code
let agreeToTermsAndConditions : UIButton = {
let button = UIButton(type: .system)
button.tintColor = .gray
button.backgroundColor = .white
button.adjustsImageWhenHighlighted = false
button.setImage(UIImage(named:"unchecked" )!, for: .normal)
button.setImage(UIImage(named:"agreechecked" )!, for: .selected)
button.addTarget(self, action: #selector(userAgreedToTerms), for: .touchUpInside)
return button
}()
The code is running on xcode 10.2.1 using swift 4 and iOS 12 deployment
Change
UIButton(type: .system)
to
UIButton(type: .custom)
Now you have complete control over the look and behavior of the button.
I am trying to create UIButton and display a text in it, as well as designate a font size but it will not show up in my Storyboard, using the setTitleColor and the titleLabel!. It will work if I comment those 2 lines out and uncomment the forgot.setTitle (That is currently commented out) however with that I cannot set the UIButton text fontsize. Any suggestions? I have added the subview to my view in my viewdidload.
let forgotPassword: UIButton = {
let forgot = UIButton()
forgot.translatesAutoresizingMaskIntoConstraints = false
//forgot.setTitle("Forgot your password?", for: .normal)
forgot.titleLabel! .font = UIFont(name:"Forgot your password?", size: 14)
forgot.setTitleColor(.white, for: .normal)
forgot.addTarget(self, action: #selector(forgot(_ :)), for: .touchUpInside)
forgot.sizeToFit()
//forgot.backgroundColor = .orange
return forgot
}()
You need to uncomment the line that sets the title - without a title it's not surprising that nothing turns up. You also need to change the font line - the name is meant to be the name of the font, not the text string to display. The below works:
let button = UIButton()
button.setTitle("Forgot your password?", for: .normal)
button.sizeToFit()
button.titleLabel?.font = UIFont.systemFont(ofSize: 14)
button.setTitleColor(.white, for: .normal)
The button is not visible because it has no text.
The problem here is that these two lines do not actually set the text, only the appearance.
forgot.titleLabel!.font = UIFont(name: "System", size: 14) // This is the font, not the text itself
forgot.setTitleColor(.white, for: .normal)
You have to call this line as well to actually set any text to appear.
forgot.setTitle("Forgot your password?", for: .normal)
sender.setBackgroundImage(imageArray[index], forState: .Normal)
sleep(2) // wait before you flip back over
sender.setBackgroundImage(cardBack, forState: .Normal)
The sleep seems to stop the image from completing its load. How can I wait for the setBackgroundImage to complete?
sender.setBackgroundImage(imageArray[index], forState: .Normal)
sender.enabled = false;
let delay_time = dispatch_time(dispatch_time_t(DISPATCH_TIME_NOW), 2 * Int64(NSEC_PER_SEC))
dispatch_after(delay_time, dispatch_get_main_queue()) {
sender.setBackgroundImage(cardBack, forState: .Normal)
sender.enabled = true;
}
Edit: Disable the button while it is showing the image.