在Storyboard中为UIButton设置边框

时间:2013-12-09 18:33:13

标签: ios objective-c uibutton storyboard uistoryboard

我无法在XCode 5中为我的按钮添加边框,而无需在代码中直接设置它们。如果没有制作自定义背景图片,是否有可能在故事板上无法做到这一点?

5 个答案:

答案 0 :(得分:72)

您可以使用密钥路径。

例如图像中描述的角半径(layer.cornerRadius)。 您将无法在故事板上看到效果,因为此参数在运行时进行评估。现在您可以使用@IBInspectable中的UIView(图片中的代码)中的swift类别在故事板上显示结果(如果您使用的是类别,请仅使用cornerRadius而不是layer.cornerRadius作为关键路径。

enter image description here


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
        }
    }
}

参考:http://nshipster.com/ibinspectable-ibdesignable/

答案 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.

长答案:

圆角UIButton

layer.cornerRadius = 10
layer.borderWidth = 1
layer.borderColor = UIColor.blue.cgColor

边界厚度

customUIView.layer.cornerRadius = 10

边框颜色

pcustomUIView.layer.borderWidth = 1

答案 3 :(得分:0)

您可以设置UIButton用户定义的运行时属性,即 borderWidth,cornerRadius,borderColor等。如图所示。 注意:-不要使用图层。属性名称之前,它将不起作用。

enter image description here

答案 4 :(得分:-3)

您可以使用以下代码:

self.addButton.layer.borderColor = [[UIColor greenColor] CGColor];

请注意:addButton是IBOutlet。