【问题标题】:Autolayout issue when showing the dropdown table显示下拉表时的自动布局问题
【发布时间】:2020-05-26 07:05:05
【问题描述】:

在我的应用程序中,我想在单击文本字段后显示一个下拉表。但是效果不好。

tableViewOne.snp.updateConstraints { (make) in

                let superViewOriginY: CGFloat = (textField.superview?.frame.origin.y)!
                let textFieldMaxY: CGFloat = textField.frame.maxY
                let navigationHeight: CGFloat = self.navigationController!.navigationBar.frame.size.height
                let textFieldHeight = textField.frame.height
                let tableViewOneTopPsition = superViewOriginY + textFieldMaxY + navigationHeight + textFieldHeight

                make.top.equalTo(tableViewOneTopPsition)
                make.left.equalTo(15)
                make.right.equalTo(-15)
                make.height.equalTo(0)
            }
            self.view.layoutIfNeeded()
            UIView .animate(withDuration: 0.4, animations: {
                self.tableViewOne.snp.updateConstraints({ (make) -> Void in
                    make.height.equalTo(170)
                })
                self.view.layoutIfNeeded()
            }, completion: nil)

通过编写此代码,它适用于 iphone 11 Max 模拟器。但是对于 iphone 8 plus 模拟器,文本字段和表格视图之间存在一些差距。` 在以下图像中,堆栈视图内有文本字段。

https://i.stack.imgur.com/JDUkw.jpg

https://i.stack.imgur.com/GkQXd.png

   First one image is the iphone 11max pro image 
   and the second one is the iphone 8 plus image

如果我会写

 make.top.equalTo(textFieldMaxY)
the i will be 

https://i.stack.imgur.com/fmNNe.jpg

请帮我解决这个问题。

【问题讨论】:

    标签: ios swift xcode uitableview


    【解决方案1】:

    添加这一行

    tableViewOne.snp.updateConstraints { (make) in
    
        make.top.equalTo(textField.snp.bottom).offset(5)
        make.left.equalTo(15)
        make.right.equalTo(-15)
        make.height.equalTo(0)
     }
    
                UIView .animate(withDuration: 0.4, animations: {
                    self.tableViewOne.snp.updateConstraints({ (make) -> Void in
                        make.height.equalTo(170)
                    })
                    self.view.layoutIfNeeded()
                }, completion: nil)
    

    【讨论】:

    • 嗨 @Jawad 添加代码后,我收到类似错误 - 致命错误:更新的约束找不到要更新的现有匹配约束:
    • 你能分享演示项目吗?
    • 表格和文本字段的视图层次结构是什么?
    • 有一个scrollview,里面有一个view,里面有stackview。
    • topSafeArea = view.safeAreaInsets.top ...将此添加到计算中...希望能解决您的问题
    【解决方案2】:

    确保您的约束是正确的。要修复您指定的错误,您可以将tableViewOnetopAnchor 约束更改为textFieldbottomAnchor。不要计算框架给topAnchor,而是给副视图的约束。

    tableViewOne.topAnchor.constraint(equalTo: textField.bottomAnchor).isActive = true
    

    添加时删除此行:

    make.top.equalTo(tableViewOneTopPsition)
    

    【讨论】:

    • 是的,我也添加了此代码。但表格视图消失了。 stackview 会发生这种情况吗?
    • 您需要删除其他约束。
    • 没有其他限制。超级视图的“前导”和“拖曳”空间 - 15。高度为 265。标签 10 的顶部空间。stackview 对齐也是“填充”,分布是“平均填充”,间距为 10。
    • @TapanRaut 我已经在我的回答中添加了您需要删除的约束。
    • 如果我将删除约束,那么它是如何工作的?应该需要一些最低限度的约束。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-08-15
    • 1970-01-01
    • 2012-09-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-25
    相关资源
    最近更新 更多