我为我的应用程序做了一个导航栏,我想在我所有的屏幕上重复使用它。
然而,问题是它没有填满分配的空间。那么,如何设置自定义导航栏视图的宽度和高度与分配给它的superview相同呢?
我的NavigationBarView.swift:
import UIKit
class NavigationBarView: UIView {
@IBOutlet var view: UIView!
required init(coder aDecoder: NSCoder){
super.init(coder: aDecoder)
NSBundle.mainBundle().loadNibNamed("NavigationBarView", owner: self, options: nil)
self.addSubview(self.view)
}
}加载我的NavigationBarView.xib。
然后在我的Main.storyboard上,我创建了一个新视图,并将该视图的类设置为"NavigationBarView“。但现在它只填满了分配空间的一半左右。
我怎样才能让它填满整个空间?
发布于 2015-07-13 14:29:04
你必须给闪屏拳头,如果它不工作,那么调用下面的方法
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
// set your custom view frame here
}发布于 2015-07-13 15:07:11
在这里找到了解决方案:https://gist.github.com/bwhiteley/049e4bede49e71a6d2e2
class CustomView : UIView {
@IBOutlet weak private var contentView:UIView!
// other outlets
override init(frame: CGRect) { // for using CustomView in code
super.init(frame: frame)
self.commonInit()
}
required init(coder aDecoder: NSCoder) { // for using CustomView in IB
super.init(coder: aDecoder)
self.commonInit()
}
private func commonInit() {
NSBundle.mainBundle().loadNibNamed("CustomView", owner: self, options: nil)
contentView.frame = self.bounds
contentView.autoresizingMask = .FlexibleHeight | .FlexibleWidth
self.addSubview(contentView)
}
}它就像一种护身符。:-)
https://stackoverflow.com/questions/31376605
复制相似问题