Swift를 사용하여 Xcode에서 사용자 정의보기를 만들기 시작했습니다. http://www.thinkandbuild.it/building-a-custom-and-designabl-control-in-swift/에있는 접근 방식을 사용하기로 결정하여 Interface Builder에서 컨트롤의 속성을 설정할 수있게했습니다.Swift에서 사용자 정의보기 : Subview의 테두리와 배경이 사라지는 원인이되는 제약
업데이트 : 사용자보기의 하위보기에 레이블을 배열하고보기와 함께 하위보기를 정렬하면서보기를 계속 작성했습니다. 나는 자동 레이아웃과 제약을 사용하여 두 레벨 모두에서 끝내고 그런 식으로 너비 문제를 해결했습니다. 그에 따라 아래 코드를 업데이트했습니다.
두 가지 문제가 남아 :
- 마지막 단계로, 내가
txtButton.setTranslatesAutoresizingMaskIntoConstraints(false)
을 설정하고 서브 뷰 '테두리와 배경 사라진 txtButton의 서브 뷰 =>에 대한 제약 조건을 설정합니다. - 고유의 크기, 즉 내가 레이아웃 0으로 높이를 설정하는 것이 좋습니다 IB에보고 된 문제 또는 (16)를 얻고,
사용자 정의보기 클래스 IB에 표시되지 않습니다 :
import UIKit
@IBDesignable public class TextButtonView: UIView {
@IBInspectable var borderColor: UIColor = UIColor.clearColor()
@IBInspectable var borderWidth: CGFloat = 0
@IBInspectable var cornerRadius: CGFloat = 0
@IBInspectable var viewBackgroundColor: UIColor = UIColor.clearColor()
@IBInspectable var mainText: String = ""
@IBInspectable var mainTextSize: CGFloat = 15.0
@IBInspectable var mainTextColor: UIColor = UIColor.blackColor()
@IBInspectable var secText: String = ""
@IBInspectable var secTextSize: CGFloat = 15.0
@IBInspectable var secTextColor: UIColor = UIColor.blackColor()
@IBInspectable var horizMargin: CGFloat = 5.0
@IBInspectable var secHorizOffset: CGFloat = 0.0
@IBInspectable var verticalMargin: CGFloat = 3.0
@IBInspectable var lineSpacing: CGFloat = 10.0
var txtButton: UIControl!
var buttonHeight: CGFloat = 0.0
#if TARGET_INTERFACE_BUILDER
override func willMoveToSuperview(newSuperview: UIView?) {
// Build the TextButton.
txtButton = TextButton(
borderColor: self.borderColor,
borderWidth: self.borderWidth,
cornerRadius: self.cornerRadius,
viewBackgroundColor: self.viewBackgroundColor,
mainText: self.mainText,
mainTextSize: self.mainTextSize,
mainTextColor: self.mainTextColor,
secText: self.secText,
secTextSize: self.secTextSize,
secTextColor: self.secTextColor,
horizMargin: self.horizMargin,
secHorizOffset: self.secHorizOffset,
verticalMargin: self.verticalMargin,
lineSpacing: self.lineSpacing,
frame: self.bounds)
// Add the TextButton as subview of this view
self.addSubview(txtButton)
// Remember height for setting intrinsic content size.
buttonHeight = txtButton.frame.size.height
// Set remaining attributes for the container view.
self.backgroundColor = UIColor.clearColor()
// Setting constraints for the subview.
txtButton.setTranslatesAutoresizingMaskIntoConstraints(false)
self.addConstraint(NSLayoutConstraint(item: txtButton, attribute: .Left, relatedBy: .Equal, toItem: self, attribute: .Left, multiplier: 1, constant: 0))
self.addConstraint(NSLayoutConstraint(item: txtButton, attribute: .Top, relatedBy: .Equal, toItem: self, attribute: .Top, multiplier: 1, constant: 0))
self.addConstraint(NSLayoutConstraint(item: txtButton, attribute: .Right, relatedBy: .Equal, toItem: self, attribute: .Right, multiplier: 1, constant: 0))
}
#else
override public func awakeFromNib() {
super.awakeFromNib()
// Build the TextButton.
txtButton = TextButton(
borderColor: self.borderColor,
borderWidth: self.borderWidth,
cornerRadius: self.cornerRadius,
viewBackgroundColor: self.viewBackgroundColor,
mainText: self.mainText,
mainTextSize: self.mainTextSize,
mainTextColor: self.mainTextColor,
secText: self.secText,
secTextSize: self.secTextSize,
secTextColor: self.secTextColor,
horizMargin: self.horizMargin,
secHorizOffset: self.secHorizOffset,
verticalMargin: self.verticalMargin,
lineSpacing: self.lineSpacing,
frame: self.bounds)
// Add the TextButton as subview of this view.
self.addSubview(txtButton)
// Remember height for setting intrinsic content size.
buttonHeight = txtButton.frame.size.height
// Set remaining attributes for the container view.
self.backgroundColor = UIColor.clearColor()
// Setting constraints for the subview.
txtButton.setTranslatesAutoresizingMaskIntoConstraints(false)
self.addConstraint(NSLayoutConstraint(item: txtButton, attribute: .Left, relatedBy: .Equal, toItem: self, attribute: .Left, multiplier: 1, constant: 0))
self.addConstraint(NSLayoutConstraint(item: txtButton, attribute: .Top, relatedBy: .Equal, toItem: self, attribute: .Top, multiplier: 1, constant: 0))
self.addConstraint(NSLayoutConstraint(item: txtButton, attribute: .Right, relatedBy: .Equal, toItem: self, attribute: .Right, multiplier: 1, constant: 0))
}
#endif
override public func intrinsicContentSize() -> CGSize {
return CGSize(width: 250, height: buttonHeight)
}
}
제어 :
import UIKit
class TextButton: UIControl {
// Designable properties and default values.
var borderColor: UIColor?
var borderWidth: CGFloat?
var cornerRadius: CGFloat?
var viewBackgroundColor: UIColor?
var mainText: String?
var mainTextSize: CGFloat?
var mainTextColor: UIColor?
var secText: String?
var secTextSize: CGFloat?
var secTextColor: UIColor?
var horizMargin: CGFloat?
var secHorizOffset: CGFloat?
var verticalMargin: CGFloat?
var lineSpacing: CGFloat?
convenience init(
borderColor: UIColor,
borderWidth: CGFloat,
cornerRadius: CGFloat,
viewBackgroundColor: UIColor,
mainText: String,
mainTextSize: CGFloat,
mainTextColor: UIColor,
secText: String,
secTextSize: CGFloat,
secTextColor: UIColor,
horizMargin: CGFloat,
secHorizOffset: CGFloat,
verticalMargin: CGFloat,
lineSpacing: CGFloat,
frame: CGRect) {
self.init(frame: frame)
self.mainText = mainText
self.mainTextSize = mainTextSize
// Button margins.
self.horizMargin = horizMargin
self.verticalMargin = verticalMargin
self.secHorizOffset = secHorizOffset
self.lineSpacing = lineSpacing
// Define the Fonts
let mainFont = UIFont(name: "Helvetica Neue", size: mainTextSize)
let secFont = UIFont(name: "Helvetica Neue", size: secTextSize)
// Create main label.
let mainLabel: UILabel = UILabel()
mainLabel.backgroundColor = UIColor.clearColor()
mainLabel.textColor = mainTextColor
mainLabel.textAlignment = .Left
mainLabel.font = mainFont
mainLabel.text = mainText
// Calculate the main label's height.
var mainLabelDummy: UILabel = mainLabel
mainLabelDummy.sizeToFit()
var mainLabelHeight: CGFloat = mainLabelDummy.frame.size.height
// Create secondary label.
let secLabel: UILabel = UILabel()
secLabel.backgroundColor = UIColor.clearColor()
secLabel.textColor = secTextColor
secLabel.textAlignment = .Left
secLabel.font = secFont
secLabel.text = secText
// Calculate the secondary label's height.
var secLabelDummy: UILabel = secLabel
secLabelDummy.sizeToFit()
var secLabelHeight: CGFloat = secLabelDummy.frame.size.height
// Add labels to view.
addSubview(mainLabel)
addSubview(secLabel)
// Set constraints for labels.
mainLabel.setTranslatesAutoresizingMaskIntoConstraints(false)
secLabel.setTranslatesAutoresizingMaskIntoConstraints(false)
self.addConstraint(NSLayoutConstraint(item: mainLabel, attribute: .Left, relatedBy: .Equal, toItem: self, attribute: .Left, multiplier: 1, constant: horizMargin))
self.addConstraint(NSLayoutConstraint(item: mainLabel, attribute: .Right, relatedBy: .Equal, toItem: self, attribute: .Right, multiplier: 1, constant: 0 - horizMargin))
self.addConstraint(NSLayoutConstraint(item: mainLabel, attribute: .Top, relatedBy: .Equal, toItem: self, attribute: .Top, multiplier: 1, constant: verticalMargin))
self.addConstraint(NSLayoutConstraint(item: secLabel, attribute: .Left, relatedBy: .Equal, toItem: self, attribute: .Left, multiplier: 1, constant: horizMargin + secHorizOffset))
self.addConstraint(NSLayoutConstraint(item: secLabel, attribute: .Right, relatedBy: .Equal, toItem: self, attribute: .Right, multiplier: 1, constant: 0 - horizMargin))
self.addConstraint(NSLayoutConstraint(item: secLabel, attribute: .Top, relatedBy: .Equal, toItem: mainLabel, attribute: .Bottom, multiplier: 1, constant: lineSpacing))
// Adjust frame to match content.
self.frame.size.height =
2 * verticalMargin
+ 2 * borderWidth
+ lineSpacing
+ mainLabelHeight
+ secLabelHeight
// Set remaining view properties.
self.layer.borderColor = borderColor.CGColor
self.layer.borderWidth = borderWidth
self.layer.cornerRadius = cornerRadius
self.backgroundColor = viewBackgroundColor
}
override init(frame: CGRect) {
super.init(frame: frame)
}
required init(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
아마도 가장 좋은 대답은 아니지만 유일한 답변입니다. :) 사건을 마무리 짓고. – jerry