0

Swift를 사용하여 Xcode에서 사용자 정의보기를 만들기 시작했습니다. http://www.thinkandbuild.it/building-a-custom-and-designabl-control-in-swift/에있는 접근 방식을 사용하기로 결정하여 Interface Builder에서 컨트롤의 속성을 설정할 수있게했습니다.Swift에서 사용자 정의보기 : Subview의 테두리와 배경이 사라지는 원인이되는 제약

업데이트 : 사용자보기의 하위보기에 레이블을 배열하고보기와 함께 하위보기를 정렬하면서보기를 계속 작성했습니다. 나는 자동 레이아웃과 제약을 사용하여 두 레벨 모두에서 끝내고 그런 식으로 너비 문제를 해결했습니다. 그에 따라 아래 코드를 업데이트했습니다.

두 가지 문제가 남아 :

  1. 마지막 단계로, 내가 txtButton.setTranslatesAutoresizingMaskIntoConstraints(false)을 설정하고 서브 뷰 '테두리와 배경 사라진 txtButton의 서브 뷰 =>에 대한 제약 조건을 설정합니다.
  2. 고유의 크기, 즉 내가 레이아웃 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") 
    } 
} 

답변

0

더 읽고 난 후에 나는 서브 뷰를 치우는에 의해 단순화, 다음과 같은 해결책을했다 : 예상대로이 지금 작동 런타임시

import UIKit 

@IBDesignable class TextButtonView: UIControl { 

    // Properties accessible in Interface Builder. 
    @IBInspectable var borderColor: UIColor = UIColor.clearColor() {didSet { updateUI() }} 
    @IBInspectable var borderWidth: CGFloat = 0 {didSet { updateUI() }} 
    @IBInspectable var cornerRadius: CGFloat = 0 {didSet { updateUI() }} 
    @IBInspectable var backgrColor: UIColor = UIColor.clearColor() {didSet { updateUI() }} 
    @IBInspectable var mainText: String = "" {didSet { updateUI() }} 
    @IBInspectable var mainTextSize: CGFloat = 20.0 {didSet { updateUI() }} 
    @IBInspectable var mainTextColor: UIColor = UIColor.blackColor() {didSet { updateUI() }} 
    @IBInspectable var secText: String = "" {didSet { updateUI() }} 
    @IBInspectable var secTextSize: CGFloat = 12.0 {didSet { updateUI() }} 
    @IBInspectable var secTextColor: UIColor = UIColor.blackColor() {didSet { updateUI() }} 
    @IBInspectable var horizMargin: CGFloat = 0.0 {didSet { updateUI() }} 
    @IBInspectable var secHorizOffset: CGFloat = 0.0 {didSet { updateUI() }} 
    @IBInspectable var verticalMargin: CGFloat = 0.0 {didSet { updateUI() }} 
    @IBInspectable var lineSpacing: CGFloat = 0.0 {didSet { updateUI() }} 

    var mainLabel: UILabel! 
    var secLabel: UILabel! 
    var textButtonHeight: CGFloat = 0.0 
    var fontName: String = "Helvetica Neue" 

    required init(coder: NSCoder) { 
     super.init(coder:coder) 
     setupUI() 
    } 

    override init(frame: CGRect) { 
     super.init(frame:frame) 
     setupUI() 
    } 

    func setupUI() { 

     // Set up static properties. 
     mainLabel = UILabel() 
     mainLabel.backgroundColor = UIColor.clearColor() 
     mainLabel.textAlignment = .Left 
     secLabel = UILabel() 
     secLabel.backgroundColor = UIColor.clearColor() 
     secLabel.textAlignment = .Left 

     // Add labels to view. 
     addSubview(mainLabel) 
     addSubview(secLabel) 

     // Update variable properties. 
     updateUI() 
    } 

    func updateUI() { 

     // Set borders and background. 
     self.layer.borderColor = borderColor.CGColor 
     self.layer.borderWidth = borderWidth 
     self.layer.cornerRadius = cornerRadius 
     self.layer.backgroundColor = backgrColor.CGColor 

     // Update main label. 
     mainLabel.textColor = mainTextColor 
     mainLabel.font = UIFont(name: fontName, size: mainTextSize) 
     mainLabel.text = mainText 

     // Update secondary label. 
     secLabel.textColor = secTextColor 
     secLabel.font = UIFont(name: fontName, size: secTextSize) 
     secLabel.text = secText 

     // Calculate view's height. 
     var mainLabelCopy: UILabel = mainLabel 
     mainLabelCopy.sizeToFit() 
     var mainLabelHeight: CGFloat = mainLabelCopy.frame.size.height 
     var secLabelCopy: UILabel = secLabel 
     secLabelCopy.sizeToFit() 
     var secLabelHeight: CGFloat = secLabelCopy.frame.size.height 
     textButtonHeight = 
      2 * verticalMargin 
      + 2 * borderWidth 
      + lineSpacing 
      + mainLabelHeight 
      + secLabelHeight 

     setNeedsUpdateConstraints() 
    } 

    override func updateConstraints() { 

     // Set constraints for labels. 
     setTranslatesAutoresizingMaskIntoConstraints(false) 
     mainLabel.setTranslatesAutoresizingMaskIntoConstraints(false) 
     secLabel.setTranslatesAutoresizingMaskIntoConstraints(false) 
     removeConstraints(constraints()) 
     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)) 
     self.addConstraint(NSLayoutConstraint(item: self, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: textButtonHeight)) 

     super.updateConstraints() 
    } 
} 

.

인터페이스 빌더에서는 실제로 이해가되지 않는 잘못된보기에 대한 경고가 표시됩니다. 보고 된 실제 좌표는 정확하지 않으며 IB의 수동 또는 자동 보정으로 수정되지 않습니다.

WarningActual Measures

그럼에도 불구하고,이 날 진행 할 수 있습니다, 그래서 제안 대답으로 게시 할 수 있습니다.

+0

아마도 가장 좋은 대답은 아니지만 유일한 답변입니다. :) 사건을 마무리 짓고. – jerry