我无法在XCode 5中为我的按钮添加边框,而无需在代码中直接设置它们。如果没有制作自定义背景图片,是否有可能在故事板上无法做到这一点?
答案 0 :(得分:72)
您可以使用密钥路径。
例如图像中描述的角半径(layer.cornerRadius
)。
您将无法在故事板上看到效果,因为此参数在运行时进行评估。现在您可以使用@IBInspectable
中的UIView(图片中的代码)中的swift类别在故事板上显示结果(如果您使用的是类别,请仅使用cornerRadius
而不是layer.cornerRadius
作为关键路径。
extension UIView {
@IBInspectable var cornerRadius: CGFloat {
get {
return layer.cornerRadius
}
set {
layer.cornerRadius = newValue
layer.masksToBounds = newValue > 0
}
}
}
以下是来自Peter DeWeese答案的类别,允许使用keypath layer.borderUIColor
来设置边框颜色。
的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
答案 1 :(得分:7)
Swift 3 如果要在使用IBInspectable时在IB中查看结果,则必须扩展UIView并将属性添加到该类,即
@IBDesignable class MyView: UIView {}
extension MyView {
@IBInspectable var cornerRadius: CGFloat {
get {
return layer.cornerRadius
}
set {
layer.cornerRadius = newValue
layer.masksToBounds = newValue > 0
}
}
@IBInspectable var borderWidth: CGFloat {
get {
return layer.borderWidth
}
set {
layer.borderWidth = newValue
layer.masksToBounds = newValue > 0
}
}
@IBInspectable var borderColor: UIColor {
get {
return UIColor.init(cgColor: layer.borderColor!)
}
set {
layer.borderColor = newValue.cgColor
}
}
}
答案 2 :(得分:0)
简短答案:
unit Unit1;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, System.Generics.Collections;
type
TForm1 = class(TForm)
btnAddRowStart: TButton;
btnAddRowEnd: TButton;
btnAddColStart: TButton;
btnAddColEnd: TButton;
Label1: TLabel;
Label2: TLabel;
btnReadValue: TButton;
btnSetValue: TButton;
txtRow: TEdit;
txtCol: TEdit;
txtReadValue: TEdit;
txtSetValue: TEdit;
procedure FormCreate(Sender: TObject);
procedure btnAddRowStartClick(Sender: TObject);
procedure btnAddRowEndClick(Sender: TObject);
procedure btnAddColStartClick(Sender: TObject);
procedure btnAddColEndClick(Sender: TObject);
procedure btnReadValueClick(Sender: TObject);
procedure btnSetValueClick(Sender: TObject);
private
Matrix: TObjectList<TList<integer>>;
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
begin
Matrix := TObjectList<TList<integer>>.Create;
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
Matrix.Free;
end;
procedure TForm1.btnReadValueClick(Sender: TObject);
var i, j: integer;
begin
i := StrToInt(txtRow.Text);
j := StrToInt(txtCol.Text);
txtReadValue.Text := Matrix[i][j].ToString;
end;
procedure TForm1.btnSetValueClick(Sender: TObject);
var i, j: integer;
begin
i := StrToInt(txtRow.Text);
j := StrToInt(txtCol.Text);
Matrix[i][j] := StrToInt(txtSetValue.Text);
end;
procedure TForm1.btnAddRowStartClick(Sender: TObject);
begin
Matrix.Insert(0, TList<integer>.Create);
end;
procedure TForm1.btnAddRowEndClick(Sender: TObject);
begin
Matrix.Add(TList<integer>.Create);
end;
procedure TForm1.btnAddColStartClick(Sender: TObject);
var Row: TList<integer>;
begin
for Row in Matrix do Row.Insert(0, 0);
end;
procedure TForm1.btnAddColEndClick(Sender: TObject);
var Row: TList<integer>;
begin
for Row in Matrix do Row.Add(0);
end;
end.
长答案:
layer.cornerRadius = 10
layer.borderWidth = 1
layer.borderColor = UIColor.blue.cgColor
customUIView.layer.cornerRadius = 10
pcustomUIView.layer.borderWidth = 1
答案 3 :(得分:0)
答案 4 :(得分:-3)
您可以使用以下代码:
self.addButton.layer.borderColor = [[UIColor greenColor] CGColor];
请注意:addButton
是IBOutlet。