No Round Rect Button in Xcode 5

No Round Rect Button in Xcode 5?

The Round Rect button from Xcode 4 and previous versions appears to have been replaced with the System Default button, which also happens to be clear. I.e. by default there is no white background with rounded corners, only the text is visible.

To make the button white (or any other colour), select the attributes inspector and scroll down to the View section:

Sample Image

Select Background and change it to White:

Sample Image

If you want rounded corners you can do so with a little bit of code.
Just ctrl-drag from the button to your .h file, call it something like roundedButton and add this in your viewDidLoad:

CALayer *btnLayer = [roundedButton layer];
[btnLayer setMasksToBounds:YES];
[btnLayer setCornerRadius:5.0f];

Xcode 5 Round Rect Buttons

  1. Open the storyboard and choose the button you want to change.
  2. Open the Identity Inspector in the Utility Panel (right panel, 3rd button on top).
  3. Add (+) a new User Defined Runtime Attribute -- Key Path: layer.cornerRadius, Type: Number, Value: {integer}.

The higher the number, the more rounded the corners. 50 is a circle for a standard button (or width/2). You won't see the change in the storyboard, but it will show at runtime.

Screenshot of added attribute

How can I make a button have a rounded border in Swift?

Use button.layer.cornerRadius, button.layer.borderColor and button.layer.borderWidth.
Note that borderColor requires a CGColor, so you could say (Swift 3/4):

button.backgroundColor = .clear
button.layer.cornerRadius = 5
button.layer.borderWidth = 1
button.layer.borderColor = UIColor.black.cgColor

How to round the corners of a button

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;

How to make a simple rounded button in Storyboard?

To do it in the storyboard, you need to use an image for the button.

Alternatively you can do it in code:

 btn.layer.cornerRadius = 10
btn.clipsToBounds = true

UIButton Round Corner not working properly on iPhone 5

It's all a matter of timing.

If you call roundCorners too soon, e.g. in viewDidLoad, the button's frame and bounds may not yet have been finalized. But your roundCorners depends on the bounds, so if you add the mask and the button is then resized as a result of layout, you will naturally get the wrong result.

iPhone button with non-rectangle shape?

Like the others say you should have a reasonable surface for registering touch events, but I'm sure you know that and have your reasons so I'll just tell you how I did it:

I needed to do this not so long ago and what I did is what Sixten Otto just suggested. There are some hints in my original question ("UPDATE" section) for getting the alpha value for your image at a certain point:

How to create a transparent window with non-rectangular buttons?

Edit: I suggest subclassing UIControl in the example below but if you don't need any special behavior on your button apart from the "non-rectangleness" of it then just subclassing a borderless UIButton set up with your PNG will do the job and require less work. You have more control on the control's behavior by subclassing UIControl and doing it the "hard way" though.

I would suggest that you subclass UIControl and override the touch event methods, then check for alpha under the tapped point and not handle the event if alpha == 0. For drawing you override drawRect: and use the NSImage's drawInRect:fromRec:operation:fraction: method in there to draw your image in the control's frame.

First you need to load the image and get a bitmap representation of it:

buttonImage = [NSImage imageNamed:@"myButtonImage"];
buttonImageRep = [[buttonImage representations] objectAtIndex:0];

Then you can draw it to the control's view: (where stateImage is a pointer to the image that must be drawn depending on if the button is pressed or not)

- (void)drawRect:(NSRect)aRect {
[stateImage drawInRect:[self bounds] fromRect:NSMakeRect(0.0,0.0,[buttonImage size].width,[buttonImage size].height)
operation:NSCompositeSourceOver fraction:1.0];
}

At this point you have drawn your button with your png image. You can override the touch event methods and handle the event if the alpha is not 0:

NSColor *colorUnderMouse = [buttonImageRep colorAtX:mouse.x y:mouse.y];
float alpha = [colorUnderMouse alphaComponent];

That's what I did and it works wonderfully. Hope this helps!

N.B: My example is for the Mac but it should also work on the iPhone, maybe with some slight modifications.

Corners of button not staying rounded when using constraints constraints

Well that was an easy fix. I just needed to round the corners in viewWillLayoutSubviews.

override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
loginBtn.roundOneSide(topCorner: .topLeft, bottomCorner: .bottomLeft)
facebookBtn.roundOneSide(topCorner: .topRight, bottomCorner: .bottomRight)
}


Related Topics



Leave a reply



Submit