다른보기를 위해 인터페이스 작성기에서 재사용 할 수있는 사용자 지정 그라디언트 레이어를 만들려고합니다.시뮬레이터에서 렌더링되지 않은 사용자 정의 요소의 하위 레이어가 인터페이스 작성기에서 렌더링
수입 UIKit
@IBDesignable
class MyGradientLayer: UIView {
let gradient = CAGradientLayer()
public override init(frame: CGRect) {
super.init(frame: frame)
gradient.colors = [startColor.cgColor, endColor.cgColor]
gradient.frame = self.bounds
gradient.locations = [0, 1]
self.layer.insertSublayer(gradient, at: 0)
}
public required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
@IBInspectable
public var startColor: UIColor = UIColor.init(red: 1, green: 0.364, blue: 0.22352, alpha: 1.0) {
didSet {
gradient.colors = [startColor.cgColor, endColor.cgColor]
}
}
@IBInspectable
public var endColor: UIColor = UIColor.init(red: 1, green: 0.1607, blue: 0.40392, alpha: 1.0) {
didSet {
gradient.colors = [startColor.cgColor, endColor.cgColor]
}
}
override func layoutSubviews() {
gradient.frame = self.bounds
gradient.locations = [0, 1]
super.layoutSubviews()
}
}
그때 내의 ViewController에있는 UIView를 추가하고, 신원 관리자에서 MyGradientLayer에 클래스를 설정 : 나는 다음과 같은 클래스를 만들었습니다. 내 작은 그라디언트보기 다음 인터페이스 빌더에서 잘 렌더링합니다. 나는 전체 화면을 차지하도록 제약 조건을 설정합니다 (수동 및 자동 제약 조건을 시도했습니다). 모두 괜찮아요, 그렇죠?
아니요, 시뮬레이터에서 그래디언트 레이어를 렌더링하지 않습니다. 그러나 IB에서 UIView의 배경색을 설정할 수 있고 전체 화면에 표시 할 수 있기 때문에보기가 있고 화면에 표시되고 있음을 알고 있습니다.
내 사용자 정의 UIView 내부의 그라디언트 레이어가 시뮬레이터에서 렌더링되지 않는 이유는 무엇입니까?
감사합니다.
이것은 그 것이다. 나는 실제로 그것을 알아 냈고 그 질문을 업데이트하려고 왔습니다. Interface Builder는 그것을 표시 할 때 init (프레임)을 사용하지만 실제로 렌더링 될 때는 코더 버전을 사용합니다. 왜 (솔직히 이렇게 많은 것들이이 환경에서 나에게 뒤얽힌 것입니까) 모르겠지만 그것은 해결책이었습니다! –