我有一个UIView
,里面有一个StackView
,我让这个UIView
出现了buttonTap
或消失了。
将UIView
和其中的StackView
添加到ViewController
:
@objc func addWishButtonTapped(){
wishView.wishNameTextField.becomeFirstResponder()
let screenSize = UIScreen.main.bounds.size
self.wishView.frame = CGRect(x: 0, y: screenSize.height, width: screenSize.width, height: self.wishView.height)
self.view.addSubview(self.wishView)
UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 1.0, initialSpringVelocity: 1.0, options: .curveEaseInOut, animations: {
self.wishView.frame = CGRect(x: 0, y: screenSize.height - self.wishView.height - self.keyboardHeight, width: screenSize.width, height: self.wishView.height)
}, completion: nil)
}
问题:
在StackView内,我想使arrangedSubview
出现/消失。
@objc func priceButtonTapped(){
self.height += CGFloat(priceViewHeight)
priceView.isHidden = false
}
如您所见,我正在height
内使用变量wishView
来跟踪其高度并将其用于上面的动画。问题是,如果我调用此函数以显示arrangedSubview
,但没有更新frame.height
中的ViewController
。
在我让视图消失并再次出现后,它只会更新frame.height
:
@objc func dismissWishView() {
let screenSize = UIScreen.main.bounds.size
UIView.animate(withDuration: 0.15, delay: 0, options: .curveEaseOut, animations: {
self.wishView.frame = CGRect(x: 0, y: screenSize.height, width: screenSize.width, height: self.wishView.height)
self.wishView.endEditing(true)
}, completion: nil)
}
这是我设置WishView
的方式:
//MARK: setupViews
func setupViews(){
addSubview(theStackView)
theStackView.topAnchor.constraint(equalTo: self.topAnchor).isActive = true
theStackView.leadingAnchor.constraint(equalTo: self.leadingAnchor).isActive = true
theStackView.trailingAnchor.constraint(equalTo: self.trailingAnchor).isActive = true
theStackView.bottomAnchor.constraint(equalTo: self.bottomAnchor).isActive = true
theStackView.addArrangedSubview(self.wishView)
wishView.addSubview(wishNameTextField)
wishView.addSubview(wishButton)
wishView.heightAnchor.constraint(equalToConstant: 70).isActive = true
self.height += CGFloat(self.wishViewHeight)
wishButton.heightAnchor.constraint(equalToConstant: 45).isActive = true
wishButton.widthAnchor.constraint(equalToConstant: 45).isActive = true
wishButton.trailingAnchor.constraint(equalTo: wishView.trailingAnchor, constant: -20).isActive = true
wishButton.centerYAnchor.constraint(equalTo: wishView.centerYAnchor, constant: 10).isActive = true
wishNameTextField.leadingAnchor.constraint(equalTo: wishView.leadingAnchor, constant: 20).isActive = true
wishNameTextField.centerYAnchor.constraint(equalTo: wishView.centerYAnchor, constant: 10).isActive = true
wishNameTextField.trailingAnchor.constraint(equalTo: wishButton.leadingAnchor, constant: -20).isActive = true
theStackView.addArrangedSubview(self.priceView)
priceView.addSubview(priceLabel)
priceView.addSubview(priceTextField)
priceView.heightAnchor.constraint(equalToConstant: 50).isActive = true
priceView.isHidden = true
priceLabel.leadingAnchor.constraint(equalTo: priceView.leadingAnchor, constant: 20).isActive = true
priceLabel.centerYAnchor.constraint(equalTo: priceView.centerYAnchor).isActive = true
priceTextField.leadingAnchor.constraint(equalTo: priceLabel.trailingAnchor, constant: 10).isActive = true
priceTextField.trailingAnchor.constraint(equalTo: priceView.trailingAnchor, constant: -195).isActive = true
priceTextField.centerYAnchor.constraint(equalTo: priceView.centerYAnchor, constant: 1).isActive = true
theStackView.addArrangedSubview(self.itemView)
itemView.addSubview(imageButton)
itemView.addSubview(priceButton)
itemView.heightAnchor.constraint(equalToConstant: 60).isActive = true
self.height += CGFloat(self.itemViewHeight)
imageButton.heightAnchor.constraint(equalToConstant: 25).isActive = true
imageButton.widthAnchor.constraint(equalToConstant: 25).isActive = true
imageButton.centerYAnchor.constraint(equalTo: itemView.centerYAnchor).isActive = true
imageButton.leadingAnchor.constraint(equalTo: itemView.leadingAnchor, constant: 20).isActive = true
priceButton.heightAnchor.constraint(equalToConstant: 25).isActive = true
priceButton.widthAnchor.constraint(equalToConstant: 25).isActive = true
priceButton.centerYAnchor.constraint(equalTo: itemView.centerYAnchor).isActive = true
priceButton.leadingAnchor.constraint(equalTo: imageButton.leadingAnchor, constant: 50).isActive = true
}
我希望我的问题很清楚。如果您需要更多说明或代码,请告诉我:)