gpt4 book ai didi

xcode - 当按下 UIBarButton 时,UIView 会回到原来的框架

转载 作者:行者123 更新时间:2023-11-28 06:51:03 25 4
gpt4 key购买 nike

在名为“courseView”的 UIView 顶部,我有一个工具栏,其中包含一个名为“expand button”的条形按钮。当我按下该按钮时,会调用 expandCourseView() 并且 View 框架会正确设置动画。但是,在动画完成后,我希望“expandButton”更改文本。当它执行此操作时, View 的框架会自动恢复到其原始形状。为什么会这样?工具栏上的按钮似乎没有记录其位置已移动。

@IBOutlet weak var expandButton: UIBarButtonItem!

@IBAction func expandButton(sender: AnyObject) {
expandCourseView()
}

func expandCourseView(){
let width = self.courseView.frame.width
let height = self.courseView.frame.height
let frameOffset : CGFloat = 200
UIView.animateWithDuration(0.5, animations: { () -> Void in
if (height < 211){
self.courseView.frame = CGRect(x: self.courseView.frame.minX, y: self.courseView.frame.minY - frameOffset, width: width, height: height + frameOffset)
}
else {
self.courseView.frame = CGRect(x: self.courseView.frame.minX, y: self.courseView.frame.minY + frameOffset, width: width, height: height - frameOffset)
}
}) { (value: Bool) -> Void in
if height < 211{
self.expandButton.title = "Close"
} else {
self.expandButton.title = "Expand"
}
}
}

最佳答案

您可能正在使用自动布局。尽管您可以直接操作帧来为 View 设置动画或移动 View ,但在某些时候(例如设置标签的文本值)会调用 setNeedsLayout() 并导致 View 恢复为约束指定的值。

您可以保持动画不变,但在完成时(或在 updateConstraints() 中)您需要修改约束以将 View 保持在新位置。

关于xcode - 当按下 UIBarButton 时,UIView 会回到原来的框架,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34891295/

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