2013-09-23 117 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