gpt4 book ai didi

ios - 我的 textView bottomAnchor 似乎不起作用?

转载 作者:行者123 更新时间:2023-11-28 11:29:53 24 4
gpt4 key购买 nike

我有一个 textView 和一条线,我设置了没有约束的线的框架,并设置了有约束的 textView 框架。只是我想要的是 textView 跟随该行,所以我将 bottomAnchor 放置到 textView 等于该行的 topAnchor。然而,当我为行设置动画时,textView 不跟随?我做错了什么?

    var button = UIButton()
var testLine = UIView()
let textView = UITextView()
var textViewBottomAnchorConstraint: NSLayoutConstraint?

override func viewDidLoad() {
super.viewDidLoad()

testLine.backgroundColor = .black
testLine.frame = CGRect(x: 0, y: 335, width: UIScreen.main.bounds.width, height: 10)
view.addSubview(testLine)

view.addSubview(textView)


textView.frame = .zero//CGRect(x: CGFloat(integerLiteral: 16), y: CGFloat(integerLiteral: 300), width: CGFloat(integerLiteral: 282), height: CGFloat(integerLiteral: 35))
textView.backgroundColor = UIColor.yellow
textView.text = ""
textView.font = UIFont(name: "Arial Rounded MT Bold", size: 15)
textView.translatesAutoresizingMaskIntoConstraints = false
textView.isHidden = false

textView.translatesAutoresizingMaskIntoConstraints = false
// textView.bottomAnchor.constraint(equalTo: testLine.topAnchor, constant: 0).isActive = true
textView.leadingAnchor.constraint(equalTo: view.layoutMarginsGuide.leadingAnchor, constant: 20).isActive = true
textView.trailingAnchor.constraint(equalTo: view.layoutMarginsGuide.trailingAnchor, constant: -20).isActive = true
textView.heightAnchor.constraint(equalToConstant: 40).isActive = true
textViewBottomAnchorConstraint = textView.bottomAnchor.constraint(equalTo: testLine.topAnchor, constant: 0)
textViewBottomAnchorConstraint?.isActive = true


UIView.animate(withDuration: 2, delay: 2, options: .curveEaseIn, animations: {

self.testLine.transform = CGAffineTransform.identity.translatedBy(x: 0, y: 30)

}) { (true) in
self.view.layoutIfNeeded()

}
}

最佳答案

正如@Vollan 正确所说的那样,动画化 transform 属性不是最佳选择。这是引自 Apple documentation :“在 iOS 8.0 及更高版本中,transform 属性不影响自动布局。自动布局根据未转换的框架计算 View 的对齐矩形。”因此 transform 属性的动画不会改变 textView 的布局。我建议您为 frame 属性而不是 transform 设置动画。

但是,如果您切换到 frame 动画,它并不能解决所有问题。如果您将动画保存在 viewDidLoad 方法中,您可能会遇到非常奇怪的行为。原因是在 viewDidLoad 中, View 本身还没有正确布局。在 viewDidLoad 中启动动画可能会导致无法预料的结果。

最后你需要调整你的动画 block 。 Apple 建议在动画 block 中应用 layoutIfNeeded inside。或者至少他们曾经推荐它然后引入了自动布局 - 观看 this WWDC video (从第 30 分钟开始)了解更多详情。

如果您应用上面的所有建议,您的代码应该如下所示:

    var button = UIButton()
var testLine = UIView()
let textView = UITextView()
var textViewBottomAnchorConstraint: NSLayoutConstraint?
var triggeredAnimation = false

override func viewDidLoad() {
super.viewDidLoad()

testLine.backgroundColor = .black
testLine.frame = CGRect(x: 0, y: 335, width: UIScreen.main.bounds.width, height: 10)
view.addSubview(testLine)

view.addSubview(textView)


textView.frame = .zero//CGRect(x: CGFloat(integerLiteral: 16), y: CGFloat(integerLiteral: 300), width: CGFloat(integerLiteral: 282), height: CGFloat(integerLiteral: 35))
textView.backgroundColor = UIColor.yellow
textView.text = ""
textView.font = UIFont(name: "Arial Rounded MT Bold", size: 15)
textView.translatesAutoresizingMaskIntoConstraints = false
textView.isHidden = false

textView.translatesAutoresizingMaskIntoConstraints = false
// textView.bottomAnchor.constraint(equalTo: testLine.topAnchor, constant: 0).isActive = true
textView.leadingAnchor.constraint(equalTo: view.layoutMarginsGuide.leadingAnchor, constant: 20).isActive = true
textView.trailingAnchor.constraint(equalTo: view.layoutMarginsGuide.trailingAnchor, constant: -20).isActive = true
textView.heightAnchor.constraint(equalToConstant: 40).isActive = true
textViewBottomAnchorConstraint = textView.bottomAnchor.constraint(equalTo: testLine.topAnchor, constant: 0)
textViewBottomAnchorConstraint?.isActive = true
}

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

// viewDidAppear may be called several times during view controller lifecycle
// triggeredAnimation ensures that animation will be called just once
if self.triggeredAnimation {
return
}
self.triggeredAnimation = true

let oldFrame = self.testLine.frame
UIView.animate(withDuration: 2, delay: 2, options: .curveEaseIn, animations: {
self.testLine.frame = CGRect(x: oldFrame.minX, y: oldFrame.minY + 30, width: oldFrame.width,
height: oldFrame.height)
self.view.layoutIfNeeded()
})
}

关于ios - 我的 textView bottomAnchor 似乎不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57130107/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com