【问题标题】:Cannot Change Properties of Controls inside an UIView that was Loadedfrom NIB无法更改从 NIB 加载的 UIView 中控件的属性
【发布时间】:2015-03-08 02:15:47
【问题描述】:

我不知道为什么会这样。

在我的应用程序中,我有

  1. ViewController.swift
  2. CustomUIView.swift
  3. CustomUIView.xib

在我的 ViewController.swift 中

var customView: CustomUIView = NSBundle.mainBundle().loadNibNamed("CustomUIView", owner: self, options: nil).first as CustomUIView;
customView.setupControl();
self.view.addSubview(customView);

在我的 CustomUIView.swift 中,我尝试更改在 CustomUIView.XIB 中创建的 UIButton 的属性,例如

@IBOutlet weak var button: UILabel!

 func setupControl()
 {
    label.frame = CGRect(x: 0 , y: 30, width: 320, height: 240); //THIS DOES NOT WORK

    label.text = "BLABLABLA"; //THIS WORKS
 }

我只能从 InterfaceBuilder 修改 UIButton 的属性。

对为什么会发生这种情况有任何想法吗?

【问题讨论】:

    标签: ios xcode swift layout


    【解决方案1】:

    您是否使用自动布局?如果是,则不能设置框架,因为约束将覆盖框架设置。要更改框架,您必须设置约束。

    【讨论】:

    • 您好,对不起,我没有正确粘贴我的代码,不过感谢您的回答。 (我现在已经修改了代码)但是问题是我无法移动标签,调整标签大小,我只能更改它的文本。
    猜你喜欢
    • 2017-10-24
    • 1970-01-01
    • 1970-01-01
    • 2011-05-05
    • 1970-01-01
    • 2014-09-11
    • 1970-01-01
    • 2011-02-08
    • 2011-07-21
    相关资源
    最近更新 更多