Text color for uilabel not setting - swift

I creating UIButton like that:
okBtn = UIButton()
okBtn.setTitle("OK", for: .normal)
okBtn.setTitle("OK", for: .selected)
okBtn.titleLabel?.textColor = .purpleLight
okBtn.backgroundColor = .red
okBtn.addTarget(self, action: #selector(didTapOKBtn), for: .touchUpInside)
self.addSubview(okBtn)
However, color is not setted, i added a screen to show how it looks.

Use title color
okBtn.setTitleColor(UIColor.blue, for: UIControlState.normal)

Use frame as required & add to your view as -
override func viewDidLoad() {
super.viewDidLoad()
let okBtn = UIButton(frame: CGRect(x: 20, y: 70, width: 50, height: 50))
okBtn.setTitle("OK", for: .normal)
okBtn.backgroundColor = .red
okBtn.setTitleColor(.white, for: .normal)
view.addSubview(okBtn)
}

Set the frame size first.
okBtn = UIButton(frame: CGRect(x: 10, y: 10, width: 100, height: 100)) //Sets the frame size on your viewController
okBtn.setTitle("OK", for: .normal)
okBtn.setTitleColor(UIColor.purple, for: .normal) //Sets the color of the text on the button
//There is no color as purpleLight. You need to set the rgb to get your desired color.
okBtn.backgroundColor = .red
okBtn.addTarget(self, action: #selector(didTapOKBtn), for: .touchUpInside)
self.view.addSubview(okBtn)

Related

Swift 5: My barbutton in navigationbar wasn't fit

Here is my image:
And I put it in rightbarbuttonitem, and it is not fit with area.
This is result
Here is my code:
let button = UIButton(type: .custom)
button.setImage(UIImage (named: "Group 4"), for: .normal)
button.frame = CGRect(x: 0.0, y: 0.0, width: 35, height: 35)
//button.addTarget(target, action: nil, for: .touchUpInside)
let barButtonItem = UIBarButtonItem(customView: button)
self.navigationItem.rightBarButtonItem = barButtonItem
Please help me!!!
Can you try this instead of your code?
let rightBarButtonItem = UIButton(frame: CGRect(x: 0, y: 0, width: 36, height: 36))
rightBarButtonItem.setBackgroundImage(UIImage(named: "Group 4"), for: .normal)
rightBarButtonItem.addTarget(self, action: #selector(actionRightBar), for: .touchUpInside)
self.navigationItem.rightBarButtonItem = UIBarButtonItem(customView: rightBarButtonItem)

Tint on an image button swift Xcode

I want to add 35% transparent tint on a button with image in my Xcode project.Suggest me how to do it in my case
My Code:
// Button
let aButt = UIButton(type: .custom)
aButt.frame = CGRect(x: X, y: Y, width: W, height: H)
aButt.tag = i
aButt.setTitle("\(peObj[EVENTS_TITLE]!)", for: .normal)
aButt.contentVerticalAlignment = .bottom
aButt.titleLabel?.font = UIFont(name: "OpenSans-Bold", size: 13)
aButt.setTitleColor(UIColor.white, for: .normal)
aButt.addTarget(self, action: #selector(popularEventButt(_:)), for: .touchUpInside)
getParseImage(object: peObj, colName: EVENTS_IMAGE1, button: aButt)
aButt.imageView?.contentMode = .scaleAspectFill
aButt.clipsToBounds = true
aButt.backgroundColor = UIColor.lightGray
aButt.layer.cornerRadius = 8

Custom Bar Button with Image And Title

I got the problem when i use Custom Bar button with image and title, I use below code
let button = UIButton(type: .system)
button.titleEdgeInsets.left = 5
button.setImage(buttonImg, for: .normal)
button.setTitle(title, for: .normal)
button.sizeToFit()
button.addTarget(self, action: #selector(self.popVC), for: .touchUpInside)
navigationItem.leftBarButtonItem = UIBarButtonItem(customView: button)
And Here is the problem snippet. Text not show clearly
Try this:
let backButton = UIButton(type: .system)
backButton.frame = CGRect(x: 0, y: 0, width: 500, height: 30)
backButton.setImage(buttonImg, for: .normal)
backButton.setTitle(title, for: .normal)
backButton.addTarget(self, action: #selector(self.popVC(_:)), for: .touchUpInside)
backButton.sizeToFit()
let backBarButton = UIBarButtonItem(customView: backButton)
self.navigationItem.leftBarButtonItem = backBarButton

Swift 3: How can I add the buttons on the UIWeb View

I am doing a project and I want to add 6 buttons on the webview , but whenever I am trying to add a button, the app doesnot show this button.
Any suggestion, help really appreciated!
Thanks
Try This-
let button = UIButton(frame: CGRect(x: 0, y: 0, width: 44, height: 44))
button.backgroundColor = UIColor.redColor()
button.addTarget(self, action: #selector(ratingButtonTapped), forControlEvents: .TouchUpInside)
self.profileVIew.addSubview(button)
func ratingButtonTapped(){
print("Button pressed")
}
I'm going to show you how to do this in code without using storyboards. Put his in your view controller class
var setupButton: UIButton = {
let button = UIButton(type: .system)
button.backgroundColor = UIColor(r: 80, g: 101, b: 161, a: 256)
button.setTitle("Register", for: .normal)
button.setTitleColor(UIColor.white, for: .normal)
button.translatesAutoresizingMaskIntoConstraints = false
button.titleLabel?.font = UIFont.boldSystemFont(ofSize: 18)
button.addTarget(self, action: #selector(handleRegister), for: .touchUpInside)
return button
}()
then in your viewDidLoad() function put
view.addSubview(setupButton)
myButton()
then outside of viewDidLoad() put
func myButton() {
setupButton.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
setupButton.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
setupButton.widthAnchor.constraint(equalTo: view.widthAnchor, constant: -24).isActive = true
setupButton.heightAnchor.constraint(equalToConstant: 150).isActive = true }
func handleRegister {
#your action here
}

How to scale image and center it on a UIButton in Swift?

var button = UIButton.buttonWithType(UIButtonType.Custom) as UIButton
var image = UIImage(named: "myimage.png") as UIImage!
button.frame = CGRectMake(0, 0, 100, 100)
button.setImage(image, forState: .Normal)
I have an image that I've set on my button, but I'd like to scale it to be smaller than the button (for example the image should be 50,50) and center in the button. How might I do this in Swift?
Xcode 8.3.1 • Swift 3.1
let button = UIButton(type: .custom)
let image = UIImage(named: "myimage.png")
func buttonTouchDown(_ button: UIButton) {
print("button Touch Down")
}
override func viewDidLoad() {
super.viewDidLoad()
button.frame = CGRect(x: 0, y: 0 , width: 100, height: 100)
button.backgroundColor = .clear
button.addTarget(self, action: #selector(buttonTouchDown), for: .touchDown)
button.setTitle("Title", for: .normal)
button.setTitleColor(.black, for: .normal)
button.setImage(image, for: .normal)
button.imageEdgeInsets = UIEdgeInsetsMake(25,25,25,25)
view.addSubview(button)
}
Swift 5, Swift 4, Swift 3
var image = UIImage(named: "myimage") as UIImage!
btnetc.setImage(image, for: .normal)
btnetc.imageView?.contentMode = .scaleAspectFill
or
btnetc.imageView?.contentMode = .scaleAspectFit
or
btnetc.imageView?.contentMode = .scaleToFill
Ther is a much easier way to do this where you don't have to deal with content insets. Here its is:
var button = UIButton.buttonWithType(UIButtonType.Custom) as UIButton
var image = UIImage(named: "myimage.png") as UIImage!
button.frame = CGRectMake(0, 0, 100, 100)
button.setImage(image, forState: .Normal)
button.contentMode = .center
button.imageView?.contentMode = .scaleAspectFit
Or via XIB after selecting your UIButton.
Just make sure your insets are all the same.
Xcode 13, Swift 5
Using storyboard, select the button, then in the size inspect click the dropdown on size just above Content inset. There is a list of sizes to select from, and this will adjust your image size(if you use system image). The default size is already set when you added the button on the storyboard to the View Controller.