2013-09-23 216 views
51

我有一个包含许多圆形矩形按钮的应用程序。但是,在xcode 5中,那些不存在。我如何获得回合按钮?他们对我的应用程序至关重要。现在它只是可按文字。我该怎么办?我计划在稍后发布此应用程序,如果这是相关的。Xcode 5圆形矩形按钮

+0

令人惊讶的是iOS的7地图应用有圆矩形信息按钮。 –

回答

153
  1. 打开故事板并选择您想要更改的按钮。
  2. 打开实用工具面板中的Identity Inspector(右侧面板,顶部的第3个按钮)。
  3. 添加(+)新的用户定义的运行属性 - 键路径:layer.cornerRadius,类型:数字,值:{整数}。

数字越高,拐角越圆。 50是标准按钮(或宽度/ 2)的圆圈。您不会在故事板中看到更改,但会在运行时显示。

Screenshot of added attribute

+8

这样的答案是我喜欢SO的原因。谢谢一堆。 – tim

+0

很好学习IB的技巧 –

10

这里有一个类似的答案,一个我给this question:

-EDIT-
将:#import <QuartzCore/QuartzCore.h>添加到您的.h文件的顶部。

如果你想圆角只是ctrl-drag从按钮到您的.h文件,调用它像roundedButton并在viewDidLoad补充一点:

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

为了使按钮的白色(或任何其他颜色),选择属性检查器,然后向下滚动到View部分,选择背景,更改为白色:

enter image description here

+4

非常重要。除非添加#import skinsfan00atg

+0

Fair point&+1,否则这将不起作用。我编辑了包含它的答案。 – Robert

+1

谢谢,不用担心,只是花了我一秒,所以我想分享。感谢更艰苦的工作:-) – skinsfan00atg

3

通过programmatically.where MyView的实现一样可以是IBOutlet中的对象。

myView.layer.cornerRadius = 5; 
myView.layer.masksToBounds = YES; 
3

雨燕2.0:

let sampleButton = UIButton(frame: CGRectMake(100,100,200,100)) 
    sampleButton.titleLabel?.text = "SAMPLE" 
    sampleButton.backgroundColor = UIColor.grayColor() 

    //Setting rounded boarder 
    sampleButton.layer.cornerRadius = 10 
    sampleButton.layer.borderWidth = 1 
    sampleButton.layer.borderColor = UIColor.blackColor().CGColor 

    self.view.addSubview(sampleButton) 
3

点击你想获得圆润按钮。 然后点击标识Inspector在右上方。 在那里您可以看到用户定义的运行属性。 点击加号(+)

see image

你不会看到查看的变化。你会看到它在运行

1

在斯威夫特3:我们可以增加它的viewDidLoad

button.layer.cornerRadius = 0.5*button.bounds.size.width 
    button.clipsToBounds = true