获取UIView高度并将其用于自身的约束 [英] Get UIView height and use it for its own constraints

查看:143
本文介绍了获取UIView高度并将其用于自身的约束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的 UIView 实际显示之前,我怎么能将它的高度呈现出来?

How can I can the height of my UIView before it is actually presented?

这是我尝试过的:

print(wishWishView.frame.size.height)

self.view.addSubview(self.wishWishView)

wishWishView.leadingAnchor.constraint(equalTo: self.view.leadingAnchor).isActive = true
wishWishView.trailingAnchor.constraint(equalTo: self.view.trailingAnchor).isActive = true

wishWishConstraint =  wishWishView.topAnchor.constraint(equalTo: self.view.bottomAnchor)
wishWishConstraint.isActive = true

UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 1.0, initialSpringVelocity: 1.0, options: .curveEaseInOut, animations: {
        self.transparentView.alpha = 0.7
        self.wishWishConstraint = self.wishWishView.topAnchor.constraint(equalTo: self.view.bottomAnchor, constant: - self.keyboardHeight)
        self.view.layoutIfNeeded()

    }, completion: nil)

问题是 wishWishView.frame.size.height 0 直到实际显示该视图。

The problem is that wishWishView.frame.size.height is 0 until the view is actually presented.

我实际上想要实现的目标是将视图隐藏在底部。我知道我可以只使用 constant:1000 ,但这是蛮力的,不是很干净。

What I actually want to achieve is to hide the view at the bottom. I know I could just use constant: 1000 but that's quite brute force and not very clean.

编辑:

过程:

1。。用户点击按钮

2。视图从滑水板的右下方显示。

2. view appears from the bottom to right above the kayboard

3。用户关闭视图

4。视图从视图底部移至底部

4. view moves to the bottom out of the view

动画

我拥有所有函数变量 func viewAppears keyboardHeight ,...),但我正在为约束苦苦挣扎。

I have all the functions and variables (func viewAppears, keyboardHeight,...), but I am struggling with the constraints.

推荐答案

所以我解决了我的问题,动画现在按预期工作。但是,实际问题仍未解决。

So I solved my problem and the animation is now working as expected. However the actual problem is still unsolved.

let height = CGFloat(130)

@objc func addWishButtonTapped(){    

    let screenSize = UIScreen.main.bounds.size
    self.wishWishView.frame = CGRect(x: 0, y: screenSize.height, width: screenSize.width, height: height)
    self.view.addSubview(self.wishWishView)

    UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 1.0, initialSpringVelocity: 1.0, options: .curveEaseInOut, animations: {
    self.transparentView.alpha = 0.7
    self.wishWishView.frame = CGRect(x: 0, y: screenSize.height - self.height - self.keyboardHeight, width: screenSize.width, height: self.height)

}, completion: nil)

您可以看到我正在使用硬编码的高度 ...

As you can see I am using the hard-coded height...

这篇关于获取UIView高度并将其用于自身的约束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆