Storyboard에서 UIButton BorderColor 변경
사용자 정의 런타임 속성에서 UIbutton에 대해 CornerRadius 및 BorderWidth를 설정했습니다. layer.borderColor를 추가하지 않고 잘 작동하고 테두리를 검정색으로 표시합니다. 그러나 layer.borderColor 를 추가 하면 작동하지 않습니다 (테두리가 표시되지 않음).
Swift의 경우 :
스위프트 3 :
extension UIView {
@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
}
}
@IBInspectable var borderColor: UIColor? {
get {
return UIColor(cgColor: layer.borderColor!)
}
set {
layer.borderColor = newValue?.cgColor
}
}
}
스위프트 2.2 :
extension UIView {
@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
}
}
@IBInspectable var borderColor: UIColor? {
get {
return UIColor(CGColor: layer.borderColor!)
}
set {
layer.borderColor = newValue?.CGColor
}
}
}
대답 변경 layer.borderColor 를 layer.borderColorFromUIColor 로 얻었습니다.
.m 파일에 코드를 추가하십시오.
#import <QuartzCore/QuartzCore.h>
@implementation CALayer (Additions)
- (void)setBorderColorFromUIColor:(UIColor *)color
{
self.borderColor = color.CGColor;
}
@end
속성 검사기의 눈금 속성
스위프트 4, 엑스 코드 9.2 - 사용 IBDesignable
및 IBInspectable
빌드 사용자 지정 컨트롤 및 실시간 미리보기 인터페이스 빌더의 디자인.
다음은 Swift의 샘플 코드입니다 UIKit
. ViewController.swift 바로 아래에 있습니다 .
@IBDesignable extension UIButton {
@IBInspectable var borderWidth: CGFloat {
set {
layer.borderWidth = newValue
}
get {
return layer.borderWidth
}
}
@IBInspectable var cornerRadius: CGFloat {
set {
layer.cornerRadius = newValue
}
get {
return layer.cornerRadius
}
}
@IBInspectable var borderColor: UIColor? {
set {
guard let uiColor = newValue else { return }
layer.borderColor = uiColor.cgColor
}
get {
guard let color = layer.borderColor else { return nil }
return UIColor(cgColor: color)
}
}
}
보기에서 검사 가능한 속성으로 이동하면 이러한 속성을 시각적으로 찾아 속성을 편집해야합니다.
변경 사항은 사용자 정의 런타임 속성에도 반영됩니다.
빌드 시간과 짜잔에 실행하십시오! 테두리가있는 명확한 둥근 버튼이 표시됩니다.
설명, 아마도 여기에 다른 답변 중 일부에서 손실 될 수 있습니다.
이 속성이 설정되지 않은 이유는 layer.borderColor
type 값 이 필요하기 때문 CGColor
입니다.
그러나 UIColor
인터페이스 빌더의 사용자 정의 런타임 속성을 통해 유형 만 설정할 수 있습니다!
따라서 Interface Builder를 통해 UIColor를 프록시 속성으로 설정 한 다음 해당 호출을 가로 채서 layer.borderColor
속성에 해당하는 CGColor를 설정해야 합니다.
This can be accomplished by creating a Category on CALayer, setting the Key Path to a unique new "property" (borderColorFromUIColor
), and in the category overriding the corresponding setter (setBorderColorFromUIColor:
).
There is a much better way to do this! You should use @IBInspectable. Check out Mike Woelmer's blog entry here: https://spin.atomicobject.com/2017/07/18/swift-interface-builder/
It actually adds the feature to IB in Xcode! Some of the screenshots in other answers make it appear as though the fields exist in IB, but at least in Xcode 9 they do not. But following his post will add them.
In case of Swift, function doesn't work. You'll need a computed property to achieve the desired result:
extension CALayer {
var borderColorFromUIColor: UIColor {
get {
return UIColor(CGColor: self.borderColor!)
} set {
self.borderColor = newValue.CGColor
}
}
}
This works for me.
Swift 3, Xcode 8.3
CALayer extension:
extension CALayer {
var borderWidthIB: NSNumber {
get {
return NSNumber(value: Float(borderWidth))
}
set {
borderWidth = CGFloat(newValue.floatValue)
}
}
var borderColorIB: UIColor? {
get {
return borderColor != nil ? UIColor(cgColor: borderColor!) : nil
}
set {
borderColor = newValue?.cgColor
}
}
var cornerRadiusIB: NSNumber {
get {
return NSNumber(value: Float(cornerRadius))
}
set {
cornerRadius = CGFloat(newValue.floatValue)
}
}
}
참고 URL : https://stackoverflow.com/questions/28854469/change-uibutton-bordercolor-in-storyboard
'program tip' 카테고리의 다른 글
한 목록을 다른 목록으로 정렬 (0) | 2020.10.26 |
---|---|
수정 : 글로벌 요소 'configuration'이 이미 선언되었습니다. (0) | 2020.10.25 |
C #에서 foreach 루프 종료 (0) | 2020.10.25 |
0과 1 사이의 신속한 임의 부동 (0) | 2020.10.25 |
Swift 사전을 콘솔에 예쁘게 인쇄하는 방법이 있습니까? (0) | 2020.10.25 |