How to set button background image size - ios

I want to create this button in my application. I have added a button to my view in storyboard. Set it's width and height constraints to 60px.
Then in code I've added corner radius and UIImage to background but the image filled the whole button background. The image is 22x22px. I want it to be centered in button like on my screenshot.
var pencilImage = UIImage(named: "pencil")!
pencilBtn.setBackgroundImage(pencilImage, forState: UIControlState.Normal)
pencilBtn.layer.cornerRadius = pencilBtn.frame.size.height / 2
pencilBtn.clipsToBounds = true
What should I code to solve this?

Set image using .setImage() method Like this:
pencilBtn.layer.cornerRadius = pencilBtn.frame.size.height / 2
pencilBtn.clipsToBounds = true
pencilBtn.setImage(UIImage("pencil"), forState: .Normal)
pencilBtn.contentMode = UIViewContentMode.Center

Related

How to set content mode when using FFImageLoading on button

I am not able to get the content mode working on button when I am setting the image using FFImageLoading library.I want to set the mode to ScaleAspectFill.
I tried setting the content mode of the button.imageview as well as the button itself.
button.ContentMode = UIViewContentMode.ScaleAspectFill;
button.ImageView.ContentMode = UIViewContentMode.ScaleAspectFill;
ImageService.Instance.LoadUrl(image_url).Into(button);
The image is not scaling and is appearing as ScaleAspectFit rather than Fill
Setting the button's ImageView's ContentMode will adjust the image's representation.
I used the code below to display two buttons. One has set the ContentMode and another doesn't:
UIButton btn = new UIButton(UIButtonType.Custom);
btn.Frame = new CoreGraphics.CGRect(50, 100, 100, 40);
btn.ImageView.ContentMode = UIViewContentMode.ScaleAspectFill;
ImageService.Instance.LoadUrl("https://img-prod-cms-rt-microsoft-com.akamaized.net/cms/api/am/imageFileData/RE1Mu3b?ver=5c31").Into(btn);
UIButton btn2 = new UIButton(UIButtonType.Custom);
btn2.Frame = new CoreGraphics.CGRect(50, 200, 100, 40);
ImageService.Instance.LoadUrl("https://img-prod-cms-rt-microsoft-com.akamaized.net/cms/api/am/imageFileData/RE1Mu3b?ver=5c31").Into(btn2);
View.AddSubview(btn);
View.AddSubview(btn2);
They have different effects:
The first button doesn't have enough width to display this image so it clips the left and right edges due to the configuration of content mode. But the second button stretches the image to fit the whole space. Both buttons have the same size.

How to add circle image on left navigation bar item

I wanted to add circle image on left navigation bar item as button. I could added, but it is not circle it is ellipse. Here is my code.
let button = UIButton();
button.downloaded(from: user?.Image);
button.frame = CGRect(x: 0, y: 0, width: 36, height:36);
button.layer.cornerRadius = button.frame.width / 2;
button.layer.masksToBounds = true;
button.imageView?.contentMode = .scaleAspectFill;
let barBtn = UIBarButtonItem(customView: button);
self.navigationItem.leftBarButtonItem = barBtn;
You can check my screen shot. Can you help me?
Issue arises when downloaded image is larger than (36, 36) it resets the frame of imageView and as result UIbutton's frame is also reset. You need to resize the image before assigning it to imageView making it smaller than (36,36).
Also don't make UIButton round, make imageView round. Otherwise touchable area of button will be reduced.
use:
button.imageView.layer.cornerRadius = button.imageView.frame.width / 2;
button.imageView.layer.masksToBounds = true;
Instead of:
button.layer.cornerRadius = button.frame.width / 2;
button.layer.masksToBounds = true;
Refer to following answer for resizing images.
How to Resize image in Swift?

Are these buttons round, or nodular?

Maybe my eyes are deceiving me, but after using this code to create round buttons for a number pad similar to iPhone's Phone app:
for subStack in (mainStackView?.arrangedSubviews)! {
for thisView in (subStack.subviews) {
if let button = thisView as? UIButton {
button.layer.borderWidth = 2
button.layer.borderColor = orangeBorderColor
button.frame.size.width = 76
button.frame.size.height = 76
button.layer.cornerRadius = 0.5 * (button.bounds.size.height)
button.layer.masksToBounds = true
// button.clipsToBounds = true
button.backgroundColor = UIColor.lightGray
button.titleLabel?.font = button.titleLabel?.font.withSize(30)
button.setTitleColor(.black, for: .normal)
}
}
}
I get things that look like this:
I've tried using clipsToBounds, maskToBounds, I've tried the button's .frame and its .bounds property as the basis for the arithmetic, but they still look nodular rather than round to me.
Can someone please offer a suggestion for smoothing them out?
Thanks!
Edit
When I add this:
print(button.frame.size as Any)
print(button.bounds.size as Any)
print(button.layer.cornerRadius as Any)
I get this in the console:
(76.0, 76.0)
(76.0, 76.0)
38.0
You can't directly set the frames of views that are laid out using constraints, and arranged subviews of a stack view must be laid out using constraints. That's your problem. Auto layout is (later) overriding your attempt to set the frame, so your corner radius doesn't match the size of your buttons.
Make a subclass of UIButton. In your subclass, override layoutSubviews to set the button's cornerRadius (and don't forget to call super.layoutSubviews).

UIButton with transparent background image is not working as it should

I created UIButton with transparent PNG.
instantHelpBtn = UIButton()
instantHelpBtn.frame = CGRectMake(10, 10, 30, 30)
instantHelpBtn.setBackgroundImage(UIImage(named: "questions1.png"), forState: .Normal)
instantHelpBtn.addTarget(self, action: "instantHelpBtnPressed", forControlEvents: .TouchUpInside)
self.view.addSubview(instantHelpBtn)
The problem is that function instantHelpBtnPressed is working only if I'm somehow pressing non-transparent parts of the background image. If I miss (and I miss often), nothing happens. How could I change this behavior?
Problem is your button frame. You are creating 30X30 UIButton where as, per Apple iOS human interface guideline, minimum size should be 44X44.
It seems to me, that 30*30 frame is just too small. Apple recommend to create ui tappable elements no smaller than 44x44 pixels. Here is a link on documentation: https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/MobileHIG/LayoutandAppearance.html
How about instead of setting it to a transparent image do...
instantHelpBtn.backgroundColor = UIColor.clearColor()
And if you want to set it to the image when clicked do...
func instantHelpBtnPressed(sender: UIButton) {
sender.setBackgroundImage(UIImage(named: "questions1.png"), forState: .Normal)
}

Custom like button IOS SDK

it is possible to customize the facebook like button SDK? I need my turn Button image or set the background of the button as image
var likeControl:FBSDKLikeControl = FBSDKLikeControl()
likeControl.objectID = fbPage
likeControl.likeControlStyle = FBSDKLikeControlStyle.BoxCount
likeControl.backgroundColor = UIColor(patternImage: UIImage(named: "fblike")!)
likeControl.frame = CGRectMake(16,20, 290, 40)
self.viewBotoes.addSubview(likeControl)
You can't assign your own background image to the FBSDKLikeControl, which is inherited from the UIControl
FBSDKLikeControl is exposing only a few properties such as foregroundColor, FBSDKLikeControlAuxiliaryPosition, FBSDKLikeControlHorizontalAlignment, likeControlStyle, preferredMaxLayoutWidth and soundEnabled

Resources