Are these buttons round, or nodular? - ios

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

Related

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

How to prevent UIButton "clipsToBounds" from clipping label?

The following code:
button.titleEdgeInsets = UIEdgeInsetsMake(100,0,0,0)
button.layer.cornerRadius = 20
button.clipsToBounds = true
makes the button's label be cut off. Of course setting
clipsToBounds = false
Makes the round edges go away.
Anyway I can have both the label and the round edges?
You can try button.layer.maskToBounds = true;

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

How do I make a circular button?

*I have this answers here : Correct with help of **Yossi
How to put buttons over UITableView which won't scroll with table in iOS***
I know answers work with NavigationControllerController
[self.navigationController.view addSubview:_btnCircle];
I learned to write in Objective C iOS app. Please help me to implement to get the Circle button display on uitableview as shown below. Or help me search keywords.
Example 1:
Example 2:
The simplest way is to add QuartzCore Framework
#import <QuartzCore/QuartzCore.h>
and then use the following code for your button:
button.layer.cornerRadius = 0.5 * button.bounds.size.width;
Now you have the round button from the square one.
In order to make any button round you need to set the cornerRadius property of the button.
Note- In order to make circle you need to make sure that height and width are equal and radius is set half of the width/height. This will make perfect round circle.
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn setFrame:CGRectMake(10, 10, 50, 50)];
btn.layer.cornerRadius = 0.5 * btn.bounds.size.width;
For Swift:
button.layer.masksToBounds = true
button.layer.cornerRadius = self.frame.width / 2
If you are creating a custom UIButton (or a custom UIView), implement the following inside the custom class:
override func layoutSubviews() {
super.layoutSubviews()
self.layer.masksToBounds = true
self.layer.cornerRadius = self.frame.width / 2
}

How to round the corners of a button

I have a rectangle image (jpg) and want to use it to fill the background of a button with rounded corner in xcode.
I wrote the following:
UIButton *button = [[UIButton buttonWithType:UIButtonTypeRoundedRect] retain];
CGRect frame = CGRectMake(x, y, cardWidth, cardHeight);
button.frame = frame;
[button setBackgroundImage:backImage forState:UIControlStateNormal];
However, the button I get with that approach doesn't have its corners rounded: it is instead a plain rectangle that looks exactly like my original image. How can I get instead an image with rounded corner to represent my button?
Thanks!
I tried the following solution with the UITextArea and I expect this will work with UIButton as well.
First of all import this in your .m file -
#import <QuartzCore/QuartzCore.h>
and then in your loadView method add following lines
yourButton.layer.cornerRadius = 10; // this value vary as per your desire
yourButton.clipsToBounds = YES;
You can achieve by this RunTime Attributes
we can make custom button.just see screenshot attached.
kindly pay attention :
in runtime attributes to change color of border follow this instruction
create category class of CALayer
in h file
#property(nonatomic, assign) UIColor* borderIBColor;
in m file:
-(void)setBorderIBColor:(UIColor*)color {
self.borderColor = color.CGColor;
}
-(UIColor*)borderIBColor {
return [UIColor colorWithCGColor:self.borderColor];
}
now onwards to set border color check screenshot
thanks
Pushing to the limits corner radius up to get a circle:
self.btnFoldButton.layer.cornerRadius = self.btnFoldButton.frame.height/2.0;
If button frame is an square it does not matter frame.height or frame.width. Otherwise use the largest of both ones.
You may want to check out my library called DCKit. It's written on the latest version of Swift.
You'd be able to make a rounded corner button/text field from the Interface builder directly:
It also has many other cool features, such as text fields with validation, controls with borders, dashed borders, circle and hairline views etc.
UIButton* closeBtn = [[UIButton alloc] initWithFrame:CGRectMake(10, 50, 90, 35)];
//Customise this button as you wish then
closeBtn.layer.cornerRadius = 10;
closeBtn.layer.masksToBounds = YES;//Important
Import QuartCore framework if it is not there in your existing project, then import #import <QuartzCore/QuartzCore.h> in viewcontroller.m
UIButton *button = [[UIButton buttonWithType:UIButtonTypeRoundedRect]];
CGRect frame = CGRectMake(x, y, width, height); // set values as per your requirement
button.layer.cornerRadius = 10;
button.clipsToBounds = YES;
First set width=100 and Height=100 of button
Objective C Solution
YourBtn1.layer.cornerRadius=YourBtn1.Frame.size.width/2;
YourBtn1.layer.borderColor=[uicolor blackColor].CGColor;
YourBtn1.layer.borderWidth=1.0f;
Swift 4 Solution
YourBtn1.layer.cornerRadius = YourBtn1.Frame.size.width/2
YourBtn1.layer.borderColor = UIColor.black.cgColor
YourBtn1.layer.borderWidth = 1.0
Try my code. Here you can set all properties of UIButton like text colour, background colour, corner radius, etc.
extension UIButton {
func btnCorner() {
layer.cornerRadius = 10
clipsToBounds = true
backgroundColor = .blue
}
}
Now call like this
yourBtnName.btnCorner()
For Objective C:
submitButton.layer.cornerRadius = 5;
submitButton.clipsToBounds = YES;
For Swift:
submitButton.layer.cornerRadius = 5
submitButton.clipsToBounds = true
If you want a rounded corner only to one corner or two corners, etc... read this post:
[ObjC] – UIButton with rounded corner - http://goo.gl/kfzvKP
It's a XIB/Storyboard subclass. Import and set borders without write code.
For Swift:
button.layer.cornerRadius = 10.0
updated for Swift 3 :
used below code to make UIButton corner round:
yourButtonOutletName.layer.cornerRadius = 0.3 *
yourButtonOutletName.frame.size.height
Swift 4 Update
I also tried many options still i wasn't able to get my UIButton round cornered.
I added the corner radius code inside the viewDidLayoutSubviews() Solved My issue.
func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
anyButton.layer.cornerRadius = anyButton.frame.height / 2
}
Also we can adjust the cornerRadius as follows:
func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
anyButton.layer.cornerRadius = 10 //Any suitable number as you prefer can be applied
}
An alternative answer which sets a border too (making it more like a button) is here ... How to set rectangle border for custom type UIButton
For iOS SWift 4
button.layer.cornerRadius = 25;
button.layer.masksToBounds = true;
You can use outlet connection and didSet function for your button on the view;
#IBOutlet weak var button: UIButton!{
didSet {
button.layer.cornerRadius = 5;
button.layer.masksToBounds = true;
}
}

Resources