UIButton with transparent background image is not working as it should - ios

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)
}

Related

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).

How to set button background image size

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

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

Can't display image in custom keyboard

i'm building custom keyboard for ios 8, so i want to display one image here but it's seems not work.
I really don't know why, here is my code:
var img = UIImageView()
img.frame = CGRectMake(0, 0, 50, 50)
img.image = UIImage(named: "interesting.jpg")
self.view.addSubview(img)
Nothing appear but if i change to display an UILabel, it's ok.
When i move this code to ViewController image display fine.
I couldn't display image in keyboard area only.
I think you could do this: UIImage(named: "interesting") and have the name of the image in the asset catalog be interesting

How do I animate a UIButton between two PNG images?

I have two PNGs I want to use as a button. How can I animate a UIButton by switching rapidly between these two images?
You can use animationImages property of your button's imageView:
myButton.imageView.animationImages =
[NSArray arrayWithObjects:[UIImage imageNamed:#"image1.png"],
[UIImage imageNamed:#"image2.png"],
nil];
myButton.imageView.animationDuration = 0.5; //whatever you want (in seconds)
[myButton.imageView startAnimating];
Your button will switch between your two images.
EDIT: As #tidbeck pointed the button needs to have an image assigned to create the imageview property.
Normally for a button you can set three "live" states:
- Normal
- Highlighted
- Selected
I don't know if this can help, but if you set one image to "Normal" and the other image to "Highlighted" you can see the two images while pushing the button.
I don't know if this effect is enough for you.
This small example shows how to set a UIButton to show an indefinite animation of a heart beating.
I have added 8 images into the Assets.xcassets, and named them like this: heart1, heart2, ..., heart8.
private func showHeartedAnimation() {
var images = [UIImage]()
for i in 1...8 { images.append(UIImage(named: "heart\(i)")!) }
self.heartButton.setImage(UIImage.animatedImage(with: images, duration: 0.2), for: .normal)
}
If you want to disable the animation at some point, just assign a simple UIImage to the same button.

Resources