使用 Swift 4 以编程方式将 UIView 添加到带有 NSLayoutConstraints 的 UITableViewController

杰森布雷迪

我正在尝试使用 NSLayoutConstraints 添加 UIView,但我不断收到类型为 NSException 错误的未捕获异常。我使用代码而不是在故事板中进行操作的主要原因是我希望它位于 UITableViewController 上的导航栏上方。

这是我目前拥有的代码。

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)

    let mainWindow: UIWindow = UIApplication.shared.keyWindow!

    let newView = UIView()
    newView.backgroundColor = UIColor.darkAqua
    newView.translatesAutoresizingMaskIntoConstraints = false

    mainWindow.addSubview(newView)

    let viewHeight = NSLayoutConstraint(item: newView, attribute: NSLayoutAttribute.height, relatedBy: NSLayoutRelation.equal, toItem: nil, attribute: NSLayoutAttribute.notAnAttribute, multiplier: 1, constant: 100)
    let viewWidth = NSLayoutConstraint(item: newView, attribute: NSLayoutAttribute.width, relatedBy: NSLayoutRelation.equal, toItem: nil, attribute: NSLayoutAttribute.notAnAttribute, multiplier: 1, constant: 100)
    let viewLeft = NSLayoutConstraint(item: newView, attribute: NSLayoutAttribute.left, relatedBy: NSLayoutRelation.equal, toItem: mainWindow, attribute: NSLayoutAttribute.left, multiplier: 1, constant: 40)
    let viewTop = NSLayoutConstraint(item: newView, attribute: NSLayoutAttribute.top, relatedBy: NSLayoutRelation.equal, toItem: mainWindow, attribute: NSLayoutAttribute.top, multiplier: 1, constant: 0)

    newView.addConstraints([viewHeight, viewWidth, viewLeft, viewTop])
}

如果我走了viewLeft,并viewTop因为它会跑好toItem:是零在NSLayoutConstraint但我认为这个问题与toItem:我尝试使用的问题有关self.viewmainWindow但它一直在抛出错误。

我已经看过很多关于如何做到这一点的例子,据我所知,我几乎是在复制它们,但它们都不是 Swift 4,所以我想知道 Swift 4 中是否有什么变化。

任何帮助都会很棒!谢谢你。

错误信息:

libc++abi.dylib:以未捕获的 NSException 类型异常(lldb)终止

塞尔瓦姆·桑卡兰

我尝试使用 self.view 而不是主窗口和不同风格的布局约束。试试这个杰森布雷迪。

override func viewDidLoad() {
    super.viewDidLoad()

    let newView = UIView()
    newView.backgroundColor = UIColor.red
    newView.translatesAutoresizingMaskIntoConstraints = false

    self.view.addSubview(newView)


    newView.leftAnchor.constraint(equalTo: self.view.leftAnchor, constant: 40).isActive = true
    newView.topAnchor.constraint(equalTo: self.view.topAnchor, constant: 0).isActive = true
    newView.widthAnchor.constraint(equalToConstant: 100).isActive = true
    newView.heightAnchor.constraint(equalToConstant: 100).isActive = true
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

使用 Storyboards 将 UIView 添加到 UITableViewController

Swift 3以编程方式创建UILabel并添加NSLayoutConstraints

如何以编程方式将UIToolbar添加到UITableViewController?

重复使用带有故事板的xib(将UIView添加到UINavigationcontroller)

带有UITableviewController Swift顶部标签的UIView

使用 ALIGN_PARENT 以编程方式将 4 个视图添加到 4 个角

将按钮添加到以编程方式创建的UIView

Swift 4-如何以编程方式使用约束后获取UIView的边界

遇到NSLayoutConstraints问题并以编程方式使用自动布局时,UIView停留在(0,0)

在Swift中的UITableViewController顶部添加一个UIView

以自定义类以编程方式创建UIView(Swift 4)

在 Swift 中以编程方式添加自动布局约束将 uiview 变为黑色

Swift中以编程方式尾随和前导约束(NSLayoutConstraints)

如何使用Swift以编程方式增加UIView的高度

以编程方式将UIImage以编程方式添加到ImageView中SWIFT

使用for循环以编程方式将元素添加到FrameLayout

将TAB BAR添加到uitableviewcontroller

在 Swift 中,以编程方式创建 UIView 并向其添加控件并使用自动布局,使控件显示在视图的父级上

从UIView获取nslayoutconstraints

使用约束将 UIView 添加到 TableViewCell 中的 ContentView

如何使用动画效果将UIButton添加到UIView

Swift 4快捷方式语法,可以将具有某些属性的多个对象添加到数组?

以编程方式添加到UITabBarController的UIView仅显示黑色

带有touchUpInside的UIButton选择器在使用Swift 4的嵌套UIView中不起作用

在不使用NavigationController的情况下将导航栏添加到UITableViewController

使用UITextView在UIView中以编程方式添加约束

使用自动布局隐藏UIView时以编程方式添加约束

如何使用 Swift 4 中的锚以编程方式向 UIScrolllView 添加视图

在Swift中以编程方式模拟UITableViewController中的选择