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
Related
I know how to create overlap shopping cart and quantity label with xib using uiimage like this
Now i'm trying to create overlap bar button items programatically but cannot figure out how to position the elements. My attempt:
My current code for bar button items:
let button: UIButton = UIButton(frame: CGRect(x: 0.0,
y: 0.0,
width: 24.0,
height: 24.0))
button.setImage(UIImage(named: "my cart", in: nil, compatibleWith: nil), for: .normal)
button.addTarget(self, action: #selector(cartButtonDidTapped), for: .touchUpInside)
let shoppingCartButton: UIBarButtonItem = UIBarButtonItem(customView: button)
shoppingCartQuantityLabel.layer.cornerRadius = 10
shoppingCartQuantityLabel.layer.masksToBounds = true
shoppingCartQuantityLabel.textColor = .white
shoppingCartQuantityLabel.backgroundColor = .red
shoppingCartQuantityLabel.textAlignment = .center
let shoppingCartQuantityLabelItem: UIBarButtonItem = UIBarButtonItem(customView: shoppingCartQuantityLabel)
navigationItem.rightBarButtonItems = [shoppingCartQuantityLabelItem, shoppingCartButton]
Idea here is to add the label as subview inside the button. You can adjust the label as per your needs from the below example,
let button: UIButton = UIButton(frame: CGRect(x: 0.0, y: 0.0, width: 24.0, height: 24.0))
button.setImage(#imageLiteral(resourceName: "my cart"), for: .normal)
let label = UILabel(frame: .init(origin: .init(x: 20, y: -8), size: .init(width: 20, height: 20)))
label.text = "12"
label.textAlignment = .center
label.textColor = .white
label.font = .systemFont(ofSize: 10)
label.backgroundColor = .red
label.layer.cornerRadius = 10
label.clipsToBounds = true
button.addSubview(label)
self.navigationItem.rightBarButtonItem = UIBarButtonItem(customView: button)
You can use this code snippet also from Github: https://gist.github.com/freedom27/c709923b163e26405f62b799437243f4#gistcomment-2236010
then set badge to your last right bar button item as below
navigationItem.rightBarButtonItems = [shoppingCartButton]
let lastBarButton = navigationItem.rightBarButtonItems?.last
lastBarButton?.setBadge(text: "10", withOffsetFromTopRight: CGPoint(x: 38, y: -3), andColor: UIColor.red, andFilled: true, andFontSize: 12)
Newbie coder and learning Swift. I want the function to be applicable for both UIButtons and couldn't figure out how to make it happen for second one.
private lazy var boostButton: UIButton = {
let button = UIButton(type: .custom)
button.frame = CGRect(x: 10, y: 10, width: 80, height: 80)
button.setImage(UIImage(named: "simsek.png"), for: .normal)
button.imageView?.contentMode = .scaleAspectFit
button.contentHorizontalAlignment = .center
button.contentVerticalAlignment = .center
button.layer.cornerRadius = 0.5 * button.bounds.size.width
button.layer.masksToBounds = true
button.addTarget(self, action: #selector(touchDown), for: [.touchDown, .touchDragEnter])
button.addTarget(self, action: #selector(touchUp), for: [.touchUpInside, .touchDragExit, .touchCancel])
return button
}()
private lazy var informationButton: UIButton = {
let button = UIButton(type: .custom)
button.frame = CGRect(x: 120, y: 10, width: 35, height: 35)
button.setImage(UIImage(named: "yenigozlukgri.png"), for: .normal)
button.imageView?.contentMode = .scaleAspectFit
button.contentHorizontalAlignment = .center
button.contentVerticalAlignment = .center
button.layer.cornerRadius = 0.5 * button.bounds.size.width
button.layer.masksToBounds = true
button.addTarget(self, action: #selector(touchDown), for: [.touchDown, .touchDragEnter])
button.addTarget(self, action: #selector(touchUp), for: [.touchUpInside, .touchDragExit, .touchCancel])
return button
}()
These are my buttons. I don't use storyboard but I believe that's not essential for the solution.
#objc func touchDown() {
animator.stopAnimation(true)
boostButton.backgroundColor = .red
//highlightedColor
}
#objc func touchUp() {
animator = UIViewPropertyAnimator(duration: 0.5, curve: .easeOut, animations: {
self.boostButton.backgroundColor = .gray
})
animator.startAnimation()
}
What I want to do is, when one of the buttons are clicked, it should perform the animation. If I add informationButton like the boostButton to my functions, both of them perform the animation even though one button is clicked. It should work for just the clicked one. How can I fix it to be functional for even more buttons ?
Use the parameter
#objc func touchDown(_ sender:UIButton) {
animator.stopAnimation(true)
sender.backgroundColor = .red
//highlightedColor
}
#objc func touchUp(_ sender:UIButton) {
animator = UIViewPropertyAnimator(duration: 0.5, curve: .easeOut, animations: {
sender.backgroundColor = .gray
})
animator.startAnimation()
}
I want to add an UIButton to the navigationItem.leftBarButtonItem.
I want the UIButton to be 24x24px and scale the Image down.
But the Button got distorted... If I change the UIImage to a smaller image everything is fine. -> see pics
What can I do apart from the scaleAspectFit ??
let userProfilePic = UIButton()
userProfilePic.frame = CGRect(x: 0, y: 0, width: 24, height: 24)
userProfilePic.setImage(UIImage(named: "profile_icon.jpg"), for: .normal)
userProfilePic.contentMode = .scaleAspectFit
userProfilePic.clipsToBounds = true
userProfilePic.layer.borderWidth = 0.5
userProfilePic.layer.borderColor = UIColor.white.cgColor
userProfilePic.layer.cornerRadius = (userProfilePic.frame.size.width) / 2
userProfilePic.addTarget(self, action: #selector(goToSettings), for: UIControlEvents.touchUpInside)
self.navigationItem.leftBarButtonItem = UIBarButtonItem(customView: userProfilePic)
Thank you!
Try this code
The is problem appears on ios 11+ , UIBarButtonItem use autolayout on ios 11+ other use frames
let userProfilePic = UIButton()
userProfilePic.frame = CGRect(x: 0, y: 0, width: 24, height: 24)
userProfilePic.setImage(UIImage(named: "profile_icon.jpg"), for: .normal)
userProfilePic.contentMode = .scaleToFill
userProfilePic.clipsToBounds = true
userProfilePic.layer.borderWidth = 0.5
userProfilePic.layer.borderColor = UIColor.white.cgColor
userProfilePic.layer.cornerRadius = (userProfilePic.frame.size.width) / 2
if #available(iOS 11, *) {
userProfilePic.widthAnchor.constraint(equalToConstant: 24.0).isActive = true
userProfilePic.heightAnchor.constraint(equalToConstant: 24.0).isActive = true
}
userProfilePic.addTarget(self, action: #selector(goToSettings), for: UIControlEvents.touchUpInside)
self.navigationItem.leftBarButtonItem = UIBarButtonItem(customView: userProfilePic)
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)
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.