【问题标题】:Loading a xib from a view subclass in Swift从 Swift 中的视图子类加载 xib
【发布时间】:2014-12-24 09:12:22
【问题描述】:

我正在尝试在 Swift 项目中重用来自 xib 的组件,但我使用的是 Objective C 逻辑。因此,子类化视图并加载 xib,然后在 视图控制器中实例化 自定义视图

将代码翻译成 Swift 并没有产生预期的结果。


CustomView.swift:

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

required init(coder aDecoder: NSCoder) {

    super.init(coder: aDecoder)
    fatalError("Error detected")

    self.commonInit()
}

private func commonInit() {
    NSBundle.mainBundle().loadNibNamed("mainBar", owner: self, options: nil)
    self.addSubview(self)
}

viewController.swift:

var bottomBar : customView = customView(frame: CGRect(x: 0, y: 250, width: 250, height: 70))
  //bottomBar.backgroundColor = UIColor.redColor()
self.view.addSubview(bottomBar)

Objective-C 我用作参考:

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self baseInit];
    }
    return self;
}

- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    if (self) {

 [[NSBundle mainBundle] loadNibNamed:@"yourXib" owner:self options:nil];

    [self addSubview:self.view];
    }
    return self;
}

CustomView * myView = [CustomView alloc]init];
    [self.view addSubView:myView];

感谢任何正确方向的评论。

【问题讨论】:

标签: ios uiview swift xib


【解决方案1】:

不是 100% 确定您的 OBJ-C 代码是否真的有效,但很可能不会。 您必须意识到这个[[NSBundle mainBundle] loadNibNamed:@"yourXib" owner:self options:nil]; 总是返回您的 IB xib 文件中的对象数组。据我所知,您实际上并没有在任何地方分配此对象。 此实现的问题可能是可以从边界获取的框架。 如果我们假设 IB 对象中只有一个对象,则此代码有效。

private func commonInit()
{
    var nibView = NSBundle.mainBundle().loadNibNamed("mainBar", owner: self, options: nil)[0] as UIView
    nibView.frame = self.bounds;
    self.addSubview(nibView)
}

【讨论】:

  • 感谢您的回答。你最初的假设是正确的。我还没有在 Objective-C 中测试过该代码。你提供的代码我一直没有成功,但我有一种直觉,我无法从UIView 子类初始化笔尖。从UIViewController 子类化时,类似的代码正在工作。你能解释一下我如何快速开发custom Tab Bar library 吗?
  • 代码是正确的,在 IB 中可能有你可能以其他方式完成的事情,它是让文件拥有你的类类型,并让 IB 附带的 UIView(或任何其他基本对象)保持原来的样子。如果有帮助,请告诉我。
  • 我同意你的观点,我的实施有问题。实际上,我最终通过创建一个 UIView 类来解决,该类以编程方式添加按钮,而不是使用 xib。
【解决方案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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-07-04
    • 1970-01-01
    • 1970-01-01
    • 2017-10-20
    • 1970-01-01
    • 2016-09-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多