Swift:将一个xib添加到UIView中

时间:2015-11-26 19:08:27

标签: ios swift uiview

我想添加自定义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。

提前感谢您的帮助。

6 个答案:

答案 0 :(得分:15)

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

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

如果您使用自动布局,那么更好的地方是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中为此视图指定您的类。之后,如果需要,可以将插座拖放到控制器类中。

enter image description here

至于我,我更喜欢第二种方法,因为您不需要以编程方式硬编码帧/创建约束,只需添加autolayout。

答案 1 :(得分:2)

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

答案 2 :(得分: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 :(得分:2)

in Latest 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)

答案 4 :(得分:1)

我自己很新,但这应该有效。

添加:

viewControllerName.addSubview(userCoinView)

删除:

userCoinView.removeFromSuperview()

答案 5 :(得分:0)

您也可以使用通用功能。供项目使用,使其全局化

<td><xsl:value-of select="name()"/></td>

使用此通用功能,例如:-

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

通用的好处是您可以使用此功能添加 View Tableviewcell 和任何其他元素。确保您使用的是像 let cell:StudentStatusTableViewCell 这样的调用类型,否则编译器将无法推断类型。

快乐编码:)

相关问题