2014-10-02 77 views
15

我可以使用IB_DESIGNABLE和/或IBInspectable在Interface Builder中設置layer.borderWidth和layer.borderColor嗎?我目前在代碼中創建我的按鈕,但我希​​望能夠在IB中設置所有這些,但我不確定這些屬性是否可以在Xcode 6中設置。我想將其設置爲IBOutlet而不是將所有這些設置在代碼中。這是我的按鈕代碼。在Interface Builder中設置UIButton圖層邊框的寬度和顏色

directions = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
directions.titleLabel.textAlignment = NSTextAlignmentCenter; 
directions.titleLabel.font = [UIFont fontWithName:@"FranklinGothicStd-ExtraCond" size:20.0]; 
[directions setTitle:@"Directions" forState:UIControlStateNormal]; 
[directions setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; 
directions.frame = CGRectMake(20, 178, 70, 70); 
directions.layer.borderWidth = 2.0f; 
directions.layer.borderColor = [UIColor whiteColor].CGColor; 
directions.clipsToBounds = YES; 
directions.backgroundColor = [UIColor clearColor]; 
[directions addTarget:self action:@selector(getDirections:) forControlEvents:UIControlEventTouchUpInside]; 
[self.view addSubview:directions]; 

我將這些數值設置建議和邊界從未在模擬器中。 編輯:我發現了爲什麼在IB中設置這些值時沒有顯示邊框。邊框顏色是CGColor,所以我必須在代碼中設置它。

回答

5

可以設置最那些在界面生成器添加運行時的屬性的元素: enter image description here

對於layer.borderWidth = 2.0F;將是:

選擇按鈕,添加一個新的屬性

的keyPath:layer.borderWidth

類型:Number 值2

這些變化會不會是界面生成器中可見,只在運行時

+0

謝謝。這有幫助,但我想知道如何使用IBInspectable和IB_DESIGNABLE,以便在IB中看到效果。 – raginggoat 2014-10-02 13:25:27

+0

我試過了,邊框沒有出現。 – raginggoat 2014-10-02 13:35:06

+0

您還必須設置borderColor以查看邊框 – Istvan 2014-10-02 13:39:23

3

是你可以 在右側點擊身份檢查員,你會發現這樣的 enter image description here

點擊+User Defined Runtime Attributes

選擇keypath和編輯

這樣寫

layer.cornerRadiusType變化的類型代碼number並設置UR所需的值這樣

你也可以改變文字顏色等等。

快樂編碼

+0

謝謝。這有幫助,但我想知道如何使用IBInspectable和IB_DESIGNABLE,以便在IB中看到效果。 – raginggoat 2014-10-02 13:33:52

+0

我剛纔試過這個,邊框沒有出現。 – raginggoat 2014-10-02 13:34:22

+0

你必須設置'[button.layer setMasksToBounds:YES];' – sreekanthk 2014-10-02 13:35:15

27

實際上,您可以通過界面構建​​器設置視圖圖層的某些屬性。我知道我可以通過xcode設置圖層的borderWidth和cornerRadius。 borderColor不起作用,可能是因爲圖層需要CGColor而不是UIColor。

你可能不得不使用字符串而不是數字,但它的工作原理!

enter image description here

但是你可以使用類別來代理性質,如layer.borderColor。(從ConventionalC CocoaPod)

的CALayer + XibConfiguration.h:

#import <QuartzCore/QuartzCore.h> 
#import <UIKit/UIKit.h> 

@interface CALayer(XibConfiguration) 

// This assigns a CGColor to borderColor. 
@property(nonatomic, assign) UIColor* borderUIColor; 

@end 

的CALayer + XibConfiguration.m:

#import "CALayer+XibConfiguration.h" 

@implementation CALayer(XibConfiguration) 

-(void)setBorderUIColor:(UIColor*)color 
{ 
    self.borderColor = color.CGColor; 
} 

-(UIColor*)borderUIColor 
{ 
    return [UIColor colorWithCGColor:self.borderColor]; 
} 

@end 

Interface Builder

其結果將是顯而易見在運行時,不在Xcode中。

+1

對於其他人碰到這個來了,如果你想迅速的版本,請上網:http://stackoverflow.com/a/27986696/130556 – RyanJM 2016-01-07 19:54:48

+0

貌似可以用自定義的用戶提供一系列的值運行的屬性的設置cornerRadius Xcode 7.3。 – 2016-05-06 12:45:06

+0

如何設置此porxy – 2017-06-27 20:58:17

相關問題