【问题标题】:Swift: Add a xib into a UIViewSwift:将 xib 添加到 UIView
【发布时间】:2016-02-29 22:58:38
【问题描述】:

我想添加一个自定义 UIView。我的自定义视图的类定义是:

class UserCoinView: UIView {
    @IBOutlet weak var userName: UILabel!
    @IBOutlet weak var coinView: UIView!
    @IBOutlet weak var coinAmount: UILabel!
    @IBOutlet weak var coinIcon: UILabel!

    override func drawRect(rect: CGRect) {
        let smartCoins = SmartShopperUtil.getSmartShopper().smartCoins

        if smartCoins != nil && smartCoins >= 0  {
            coinAmount.text = String(smartCoins!)
            coinView.backgroundColor = SmartShopperUtil.getSmartCoinBackgroundColor(SmartShopperUtil.getSmartShopper().smartCoins!)
        }

        userName.text = SmartShopperUtil.getSmartShopperNameWithFullName(SmartShopperUtil.getSmartShopper().name)
        coinIcon.text = AEVIcons.AEV_SMART_COIN
    }
}

我在ViewController中添加了一个View 我要添加这个视图,并且我已经设置了这个视图的自定义类为UserCoinView。之后,我与 ViewController 建立了连接,在这个 ViewController 中我不知道要做什么才能显示我的自定义 UIView。

提前感谢您的帮助。

【问题讨论】:

    标签: ios swift uiview


    【解决方案1】:

    有几种方法可以做到这一点。

    以编程方式添加为子视图。

    如果你使用自动布局,更好的地方是viewDidLayoutSubviews 方法。

    var myCustomView: UserCoinView? // declare variable inside your controller
    override func viewDidLayoutSubviews() {
      super.viewDidLayoutSubviews()
      if myCustomView == nil { // make it only once
        myCustomView = NSBundle.mainBundle().loadNibNamed("UserCoinView", owner: self, options: nil).first as? UserCoinView
        myCustomView.frame = ...
        self.view.addSubview(myCustomView) // you can omit 'self' here
        // if your app support both Portrait and Landscape orientations 
        // you should add constraints here
      }
    }
    

    在 InterfaceBuilder 中添加为子视图。

    您只需在情节提要中为您的控制器放置一个空视图,并在 Identity Inspector 中为该视图分配您的类。之后,如果需要,您可以将插座拖放到控制器类中。

    就我而言,我更喜欢第二种方法,因为您不需要以编程方式硬编码框架/创建约束,只需添加自动布局。

    【讨论】:

    • 为 Swift 5 更新:myCustomView = Bundle.main.loadNibNamed("MyCustomView", owner: self, options: nil)?.first as?我的自定义视图
    【解决方案2】:

    你可以试试这个

    override init(frame: CGRect) {
       super.init(frame: frame)
        loadViewFromNib ()
     }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        loadViewFromNib ()
    }
    
    func loadViewFromNib() {
    
    let view = UINib(nibName: "CreditCardExperyView", bundle: NSBundle(forClass: self.dynamicType)).instantiateWithOwner(self, options: nil)[0] as! UIView
    
    view.frame = bounds
    
    view.autoresizingMask = [.FlexibleWidth, .FlexibleHeight]
    
    self.addSubview(view);
    
    }
    
       // Call subview 
       let creditCardView : CreditCardExperyView = CreditCardExperyView(frame: CGRect(x: 0, y: UIScreen.mainScreen().bounds.size.height - 280, width: UIScreen.mainScreen().bounds.size.width, height: 280))
    
      selfView.addSubview(creditCardView)
    

    【讨论】:

    • 这行得通! :)
    【解决方案3】:

    将其作为子视图添加到 UIViewController 或 UITableViewController 的内容视图(或视图控制器的视图层次结构中的其他视图)。

    【讨论】:

      【解决方案4】:

      在最新的 swift -> 例如:

          let headerView = Bundle.main.loadNibNamed("SectionHeaderView", owner: 
          self, options: nil)?.first as? SectionHeaderView 
          self.view.addSubview(headerView!)
          headerView?.frame = CGRect(x:0, y: 0, width: view.frame.width, height: 50.0)
      

      【讨论】:

        【解决方案5】:

        我自己很新,但这应该可以。

        添加:

        viewControllerName.addSubview(userCoinView)
        

        删除:

        userCoinView.removeFromSuperview()
        

        【讨论】:

          【解决方案6】:

          你也可以使用泛型函数。供项目使用,使其全球化

          struct GenericFunctions {
          static func addXIB<T>(xibName: String) -> T? {
                  return  Bundle.main.loadNibNamed(xibName, owner: self, options: nil)?.first as? T
              }
          }
          

          使用这个通用函数,例如:-

          if let cell: StudentStatusTableViewCell = GenericFunctions.addXIB(xibName: "StudentStatusTableViewCell") {
          
                      return cell
                  } else {
                      return UITableViewCell()
                  }
          

          generic 的好处是你可以使用这个函数来添加 ViewTableviewcell 和任何其他元素。确保您在调用中使用类型,如 let cell: StudentStatusTableViewCell 否则编译器不会推断类型。

          编码愉快:)

          【讨论】:

            猜你喜欢
            • 2018-10-07
            • 2016-06-10
            • 1970-01-01
            • 1970-01-01
            • 2011-06-15
            • 1970-01-01
            • 1970-01-01
            • 2015-05-23
            • 2011-10-11
            相关资源
            最近更新 更多