[英]Add a xib file to ad UIView
I'm having this method where i'm creating a draggableView. 我在创建DraggableView的时候有这种方法。 I've beside this created a xib file, which i would like to add as a subview to this draggableView.
我在此旁边创建了一个xib文件,我想将其作为子视图添加到此draggableView中。 How can i do this?
我怎样才能做到这一点? The reason i want to do this is that i'm going to create a lot of xib files so the content will vary.
我要执行此操作的原因是,我将创建很多xib文件,因此内容会有所不同。
override func viewDidLoad() {
super.viewDidLoad()
self.loadCards()
}
func createDraggableViewWithDataAtIndex(index: NSInteger) -> DraggableView {
let draggableView = DraggableView(frame: CGRectMake(12, 12, self.view.frame.width-24, self.view.frame.height-24-49-64))
draggableView.layer.borderWidth = 1
draggableView.layer.borderColor = UIColor.lightGrayColor().colorWithAlphaComponent(0.5).CGColor
draggableView.delegate = self
let subviewArray = NSBundle.mainBundle().loadNibNamed("AdViewController", owner: self, options: nil)
let mainView = subviewArray[0] as! UIView;
draggableView.addSubview(mainView);
return draggableView
}
func loadCards() -> Void {
if exampleCardLabels.count > 0 {
let numLoadedCardsCap = exampleCardLabels.count > MAX_BUFFER_SIZE ? MAX_BUFFER_SIZE : exampleCardLabels.count
for var i = 0; i < exampleCardLabels.count; i++ {
let newCard: DraggableView = self.createDraggableViewWithDataAtIndex(i)
allCards.append(newCard)
if i < numLoadedCardsCap {
loadedCards.append(newCard)
}
}
for var i = 0; i < loadedCards.count; i++ {
if i > 0 {
self.view.insertSubview(loadedCards[i], belowSubview: loadedCards[i - 1])
} else {
self.view.addSubview(loadedCards[i])
}
cardsLoadedIndex = cardsLoadedIndex + 1
}
}
}
After 后
That's how you add a custom view from xib 这就是您从xib添加自定义视图的方式
let subviewArray = NSBundle.mainBundle().loadNibNamed("MyCustomView", owner: self, options: nil)
let mainView = subviewArray[0] as! UIView;
draggableView.addSubview(mainView);
If I understand well you want to add the mainView of your xib as a subView of your draggableView? 如果我很了解,您想将xib的mainView添加为draggableView的子视图吗? If yes just get the view from xib:
如果是,请从XIB获取视图:
let view = UINib(
nibName: nibNamed,
bundle: bundle
).instantiateWithOwner(nil, options: nil)[0] as? UIView
And then add it as Subview to your draggrableView 然后将其作为Subview添加到您的draggrableView
draggableView.addSubview(view)
hope it helps. 希望能帮助到你。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.