Swift UIButton Image size distorted in BarButtonItem - swift

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)

Related

How to create overlapped bar button item programatically

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)

How do I place the stepper value between the - and + signs?

I am programatically trying to customize a UIStepper. I'd like to place the stepper.value between the - and + buttons and I want to change the background of the stepper to white.
How would I do that?
Here is what I have that does not work:
let stepper = UIStepper(frame: CGRect(x: 0, y: 0, width: 200, height: 40))
stepper.minimumValue = 0
stepper.center = self.contentView.center
stepper.backgroundColor = .white
stepper.layer.backgroundColor = UIColor.white.cgColor
stepper.stepValue = 1
stepper.tintColor = UIColor(hex: Constants.Colors.primary)
stepper.setIncrementImage(UIImage(named: "icon_cart_plus"), for: .normal)
stepper.setDecrementImage(UIImage(named: "icon_cart_minus"), for: .normal)

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

add gesture recognizer uivew navigation bar swift not working

I have a navigation controller with a navigation bar, I have added a UIView with subview of imageView and UILabel for titleView.
I need to be able to click on that view to do something else with addGestureRecognizer on that view but nothing is printed on the console.
The UIImageView has to be next to the UILabel
Here is the code I tried so far
private func setupNavBarWithUser() {
let titleView = UIView()
let width = titleView.sizeThatFits(CGSize(width: CGFloat.greatestFiniteMagnitude, height: CGFloat.greatestFiniteMagnitude)).width
titleView.frame = CGRect(origin:CGPoint.zero, size:CGSize(width: width, height: 500))
let profileImageView = UIImageView()
profileImageView.translatesAutoresizingMaskIntoConstraints = false
profileImageView.contentMode = .scaleAspectFill
profileImageView.layer.cornerRadius = 20
profileImageView.clipsToBounds = true
ImageService.getImage(withURL: NSURL(string: (user?.pictureURL)!)! as URL) { (image) in
profileImageView.image = image
}
titleView.addSubview(profileImageView)
profileImageView.leftAnchor.constraint(equalTo: titleView.leftAnchor).isActive = true
profileImageView.centerYAnchor.constraint(equalTo: titleView.centerYAnchor).isActive = true
profileImageView.widthAnchor.constraint(equalToConstant: 40).isActive = true
profileImageView.heightAnchor.constraint(equalToConstant: 40).isActive = true
let nameLabel = UILabel()
titleView.addSubview(nameLabel)
nameLabel.text = user?.first_name
nameLabel.textColor = .white
nameLabel.translatesAutoresizingMaskIntoConstraints = false
nameLabel.leftAnchor.constraint(equalTo: profileImageView.rightAnchor, constant: 8).isActive = true
nameLabel.centerYAnchor.constraint(equalTo: profileImageView.centerYAnchor).isActive = true
nameLabel.rightAnchor.constraint(equalTo: titleView.rightAnchor).isActive = true
nameLabel.heightAnchor.constraint(equalTo: profileImageView.heightAnchor).isActive = true
titleView.centerXAnchor.constraint(equalTo: titleView.centerXAnchor).isActive = true
titleView.centerYAnchor.constraint(equalTo: titleView.centerYAnchor).isActive = true
self.navigationItem.titleView = titleView
let recognizer = UITapGestureRecognizer(target: self, action: #selector(self.testing))
titleView.isUserInteractionEnabled = true
titleView.addGestureRecognizer(recognizer)
}
#objc func testing() {
print("hello")
}
Hope someone can help me with this problem, much thank you !!
UPDATE this is where I need to add a gesture recognizer
Add this method in viewDidLoad/viewWillAppear
if let navigationBar = self.navigationController?.navigationBar {
// create a button
let button = UIButton(type: .custom)
button.frame = CGRect(x: navigationBar.frame.width/2-20, y: 0, width: 100, height: 40)
button.setTitleColor(.red, for: .normal)
button.setTitle("Button", for: .normal)
button.addTarget(self, action: #selector(self.testing), for: .touchUpInside)
// create a imageview
let profileImageView = UIImageView()
profileImageView.contentMode = .scaleAspectFill
profileImageView.layer.cornerRadius = 20
profileImageView.clipsToBounds = true
profileImageView.frame = CGRect(x: navigationBar.frame.width/2-60, y: 0, width: 40, height: 40)
profileImageView.image = UIImage(named: "heart")
// Add two elements to navigationbar
navigationBar.addSubview(profileImageView)
navigationBar.addSubview(button)
}

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.