如何用Swift编程增加UIView的高度

vkc1a9a2  于 2023-06-21  发布在  Swift
关注(0)|答案(6)|浏览(120)

在IB中,我有一个viewController,其中有一个segmentedControl和两个containerViews。在每个containerView中,我都有一个tableView。从每个tableView中,我推一个detailView。我的汽车布局在IB。
一旦我选择了一个片段,我就会看到相应的tableView,我选择了一个单元格,正确的detailView就会被推送到场景中。
问题是一旦detailView被推到segmentedControl上,它仍然是可见的。我决定隐藏segmentedControl,它可以工作,但那里有一个很大的空白空间。我尝试以编程方式增加detailView视图的大小,但它没有扩展。据我所知,这是因为我在故事板中做了最初的限制。

    • 如何以编程方式获取detailView的视图以进行扩展?**

代码:

DetailViewController: UIViewController{

override func viewDidLoad() {
        super.viewDidLoad()

 //this isn't increasing the view's size
 self.view.frame.size.height += 20.0

 //doesn't work. I get an error on the last argument
 self.view.frame = CGRect(x: 0, y: 0, width: self.view.frame.width, height: self.view.frame.height += 20.0)

 //doesn't work. I get an error on the last argument
 self.view.frame = CGRect(x: 0, y: 0, width: self.view.frame.width, height: self.view.frame.size.height += 20.0)
 }

}

错误:

//Error for the last argument- height: *self.view.frame.height += 20.0*
self.view.frame = CGRect(x: 0, y: 0, width: self.view.frame.width, height: self.view.frame.height += 20.0)

可变运算符的左侧不是可变的:'height'是只获取属性

//Error for the last argument- *self.view.frame.size.height += 20.0*:
self.view.frame = CGRect(x: 0, y: 0, width: self.view.frame.width, height: self.view.frame.size.height += 20.0)

无法使用类型为“(x:Int,y:Int,width:CGFloat,高度:())'

ogsagwnx

ogsagwnx1#

您需要为细节视图的高度约束创建一个出口,然后可以通过编程方式调整高度,如myHeightConstraint.constant += extraHeight,其中extraHeight是一个数字,表示您希望细节视图高多少。您还可能需要在之后调用self.view.layoutIfNeeded()
若要从约束创建出口,请在情节提要编辑器中控制从约束拖动到代码中(就像处理UIView的出口一样)。
你是对的-因为你使用的是自动布局和约束,调整也需要约束。设置原始帧可能会导致意外行为。如果你有任何其他问题或困难,请告诉我。

yruzcnhs

yruzcnhs2#

CGRect初始化器中,不应该使用+=,而应该使用+
变更:

self.view.frame = CGRect(x: 0, y: 0, width: self.view.frame.width, height: self.view.frame.height += 20.0)

致:

self.view.frame = CGRect(x: 0, y: 0, width: self.view.frame.width, height: self.view.frame.height + 20.0)

您不应该尝试在初始化器中更改self.view.frame.height的值。

3z6pesqy

3z6pesqy3#

对我来说,程序化的解决方案不需要我为每一个我可能想要修改的高度限制创建一个出口。这里有一个代码解决方案,对我来说很好。无论原始约束是在脚本上创建的还是以编程方式创建的,它都应该起作用。它还提供了一个选项,可以根据需要对更改进行动画处理--尽管我不确定如果视图嵌套在复杂的层次结构中,这是否有效。

extension UIView {
    func setHeight(_ h:CGFloat, animateTime:TimeInterval?=nil) {

        if let c = self.constraints.first(where: { $0.firstAttribute == .height && $0.relation == .equal }) {
            c.constant = CGFloat(h)

            if let animateTime = animateTime {
                UIView.animate(withDuration: animateTime, animations:{
                    self.superview?.layoutIfNeeded()
                })
            }
            else {
                self.superview?.layoutIfNeeded()
            }
        }
    }
}
svujldwt

svujldwt4#

Swift 4.2

我以编程方式为您创建一个示例:

var flowHeightConstraint: NSLayoutConstraint?

    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .white

        view.addSubview(button)
        button.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
        button.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
        button.widthAnchor.constraint(equalToConstant: 100).isActive = true
        flowHeightConstraint = button.heightAnchor.constraint(equalToConstant: 30)
        flowHeightConstraint?.isActive = true
    }

    @objc func animateButtonTapped() {
        UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 0.5, options: .curveEaseOut, animations: {
            self.flowHeightConstraint?.constant = 100
            self.view.layoutIfNeeded()
        }, completion: nil)
    }

    lazy var button: UIButton = {
       let button = UIButton()
        button.translatesAutoresizingMaskIntoConstraints = false
        button.backgroundColor = .green
        button.addTarget(self, action: #selector(animateButtonTapped), for: .touchUpInside)
        return button
    }()

这行代码创建了一个按钮,点击后可以改变按钮的高度。
希望对您有所帮助 :)

sirbozc5

sirbozc55#

首先删除约束。我在界面构建器中有约束,重新定位也有效,我不明白为什么大小更改不适用。因此,在手动更改视图大小时应避免约束。

kqlmhetl

kqlmhetl6#

使用self.view.frame.size.height = 80

相关问题