No Round Rect Button in Xcode 5?

2019-01-16 10:41发布

问题:

Is drag and drop of round rect button no longer available in Xcode 5? I can't seem to find it in the Interface Builder. I was guessing that this is one of the changes in iOS 7, but I just wanted to make sure.

回答1:

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:

Select Background and change it to White:

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];


回答2:

Can also make the rounded rect within the storyboard.



回答3:

This was too long for a comment in @Robert's answer, but I just wanted to add regarding the statement: "The Round Rect button from Xcode 4...appears to have been replaced...".

Confirming that it definitely has been replaced:

The rounded rectangle button is deprecated in iOS 7. Instead, use a system button—that is, a UIButton object of type UIButtonTypeSystem.

iOS 7 system buttons don’t include a bezel or a background appearance. A system button can contain a graphical symbol or a text title, and it can specify a tint color or receive its parent’s color.

...

If you need to display a button that includes a bezel, use a button of type UIButtonTypeCustom and supply a custom background image.

Apple iOS 7 Transition Guide, p. 45, "Rounded Rectangle Button"

So, Apple's recommendation is to use a background image.



回答4:

Actually in ios 7 the total UI has been changed for basic controls. If still you want to have a rounded rect button, you have to go for the coregraphical property of UIView subclasses i.e; layer property.

buttonObj.layer.cornerRadius = 5.0f;//any float value

to show the effect you have to give some background color to buttonObj

if you want to set the border width

buttonObj.layer.borderWidth = 2.0f;//any float value

you can also give the color for border

buttonObj.layer.borderColor = [[UIColor greenColor]CGColor];

Note: Here we have to use CGColor for the layers because layers are the core graphical properties of UIViews