【问题标题】:iOS 12: How to add UIView to UIViewController programatically?iOS 12:如何以编程方式将 UIView 添加到 UIViewController?
【发布时间】:2018-10-12 10:37:24
【问题描述】:

我用老方法将UIView注入UIViewController,但是升级到iOS 12后就不行了,报错: libc++abi.dylib: terminating with uncaught exception of type NSException

这里是代码

class SlideOutMenu: UIViewController {
    var contentView:UIView!

    func setContentViewIn(view: UIView) {
        self.contentView = view
        self.view.addSubview(self.contentView)
    }
}

然后

class MainController: UIViewController {
    var slide = SlideOutMenu()
    var viewToAdd = ViewToAdd() // extends UIView
    override func viewDidLoad()
    {
        super.viewDidLoad()
        slide.setContentViewIn(viewToAdd)
    }
}

它不起作用,但如果我这样声明,它会起作用

class SlideOutMenu: UIViewController {
    var contentView:UIView!
    convenience init(childView: UIView) {
        self.init(nibName:nil, bundle:nil)
        self.contentView = childView
    }

    override func viewDidLoad() {
        view.addSubview(self.contentView)
    }
}

并使用

SlideOutMenu(childView: UIView())

有效。

但是像这样,它没有。这很奇怪

SlideOutMenu(childView: viewToAdd)

【问题讨论】:

  • 我认为崩溃与视图无关。在它的核心你只是添加一个子视图。
  • self.contentView = view / view.addSubview(self.contentView) 您正在将视图作为子视图添加到自身。那永远行不通。您可能想要更改 self.view.addSubview(self.contentView) 中的最后一行。
  • @danypata 我通过将其他视图实例传递给它来初始化视图。检查此代码:github.com/chidori-app/CDRTranslucentSideBar/blob/master/… 在 iOS12 之前它工作正常
  • 它可能有效,但不应该这样做。看起来他们现在已经修好了。
  • @TomSawyer 您缺少self.viewsetContentViewIn 中的参数名称会覆盖控制器的view 属性。请将 setContentViewIn 的最后一行更改为 self.view.addSubview(self.contentView)

标签: ios swift uiview uiviewcontroller


【解决方案1】:

检查以下在 iOS 12 中工作的代码* 导入 UIKit

类视图控制器:UIViewController {

var view1 : UIView!
override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    view1 = UIView.init(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
    view1.center = self.view.center
    view1.backgroundColor = .red
    self.view.addSubview(self.view1)
}

} *

【讨论】:

    【解决方案2】:

    以下是我如何以编程方式构建视图的示例:

    class InformationController: UIViewController {
    
        let labelDescription: UILabel = {
            let label = UILabel()
    
            label.text = "Label Name"
            // this is important for auto layout to work
            label.translatesAutoresizingMaskIntoConstraints = false
    
            return label
        }()
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            setupView()
        }
    
        private func setupView() {
            // add the view
            view.addSubview(labelDescription)
    
            // set the auto layout and enable the constraints
            labelDescription.topAnchor.constraint(equalTo: view.topAnchor, constant: 20).isActive = true
            labelDescription.leftAnchor.constraint(equalTo: view.leftAnchor, constant: 10).isActive = true
            labelDescription.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
            labelDescription.heightAnchor.constraint(equalToConstant: 25).isActive = true
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-02
      • 2014-04-03
      • 1970-01-01
      • 1970-01-01
      • 2011-06-08
      相关资源
      最近更新 更多