web-dev-qa-db-ja.com

Interface BuilderでUIButtonレイヤーの境界線の幅と色を設定

IB_DESIGNABLEやIBInspectableを使用して、Interface Builderでlayer.borderWidthとlayer.borderColorを設定できますか?現在、コードでボタンを作成していますが、これをすべてIBで設定できるようにしたいのですが、Xcode 6でこれらのプロパティをそのように設定できるかどうかわかりません。これをIBOutletにしたいと思いますこのすべてをコードで設定する代わりに。これが私のボタンコードです。

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なので、コードで設定する必要がありました。

17
raginggoat

実際には、インターフェイスビルダーを使用してビューのレイヤーのプロパティを設定できます。 Xcodeを使用してレイヤーのborderWidthとcornerRadiusを設定できることを知っています。おそらく、レイヤーがUIColorではなくCGColorを必要としているため、borderColorは機能しません。

数字の代わりに文字列を使用する必要があるかもしれませんが、機能します!

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ではなく、実行時に明らかになります。

31
Baig

これらのほとんどは、要素にランタイム属性を追加するインターフェイスビルダーで設定できます。 enter image description here

Layer.borderWidth = 2.0fの場合;だろう:

ボタンを選択して、新しい属性を追加します

keypath:layer.borderWidth

タイプ:数値2

これらの変更は、実行時にのみ、インターフェイスビルダー内に表示されません。

5
Istvan

はい、右側のIDインスペクタをクリックすると、次のようになります enter image description here

+ in User Defined Runtime Attributes

keypathを選択して編集します

このようなコードを書く

layer.cornerRadiusおよびTypeでタイプをnumberに変更し、このようにurに必要な値を設定します

また、テキストの色なども変更できます。

ハッピーコーディング

5
sreekanthk