我写了这个示例程序,试图弄清楚发生了什么。我有一个文本字段,点击后,显示一个子视图,而不是显示键盘。在子视图中,有一个关闭按钮,用于移动子视图,使顶部与屏幕底部对齐。
这样我就可以在故事板中构建子视图,子视图与屏幕的下边缘对齐,并在启动时与hidden
对齐。 viewDidLoad
然后调用hideSubView
将其在屏幕上移开。
我在每次轮班之前和之后都有println
来告诉我子视图的位置。
我遇到的问题是数字没有加起来。我特意告诉swift将Y偏移210,但不知何故它只将它移动了68.5,这是 NOT 屏幕的底部(请注意,这不是通过将子视图设置为文本字段的输入视图来完成的)。然而,当我点击文本字段时,子视图从下往上动画,而不是中间点。谁能解释为什么这两件事中的任何一件都在发生?另一件事是 - 初始minY的457.0来自哪里?我检查了子视图的初始高度,奇怪的是143(不是141.5)。
控制台输出(// - 由我提供以澄清的行)
//hideSubview called from viewDidLoad
Before. minY:457.0 screenheight:667.0 offset:210.0
After. minY:525.5 screenheight:667.0 offset:210.0 //NOTE:457+210 is 667, not 525.5
//1st showSubView called by clicking on myTextField
Before. minY:525.5 screenheight:667.0 subview Height:141.5
After. minY:525.5 subView height:141.5
//hideSubView called by clicking on dismiss button
Before. minY:525.5 screenheight:667.0 offset:141.5
After. minY:667.0 screenheight:667.0 offset:141.5
//2nd showSubView called by clicking on myTextField
Before. minY:667.0 screenheight:667.0 subview Height:141.5
After. minY:525.5 subView height:141.5
//hideSubView called by clicking on dismiss button
Before. minY:525.5 screenheight:667.0 offset:141.5
After. minY:667.0 screenheight:667.0 offset:141.5
Main.storyboard 的 ViewController.swift
import UIKit
class ViewController: UIViewController, UITextFieldDelegate {
@IBOutlet weak var myTextField: UITextField!
@IBOutlet weak var mySubView: UIView!
var screenSize = UIScreen.mainScreen().bounds
var subViewIsVisible = false
override func viewDidLoad() {
super.viewDidLoad()
myTextField.delegate = self
hideSubView()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
@IBAction func dismissButtonClicked(sender: AnyObject) {
if subViewIsVisible {
hideSubView()
}
}
func textFieldDidBeginEditing(textField: UITextField) {
myTextField.endEditing(true)
if !subViewIsVisible {
showSubView()
}
}
func hideSubView () {
var offsetAmount = screenSize.height - mySubView.frame.minY
println("Before. minY:\(mySubView.frame.minY) screenheight:\(screenSize.height) offset:\(offsetAmount)")
UIView.animateWithDuration(0.25, delay: 0.0, options: UIViewAnimationOptions.CurveEaseOut , animations:
{
self.mySubView.frame.offset(dx: 0, dy: offsetAmount)
}, completion: {_ in
self.subViewIsVisible = false
self.mySubView.hidden = true
println("After. minY:\(self.mySubView.frame.minY) screenheight:\(self.screenSize.height) offset:\(offsetAmount)")
}
)
}
func showSubView () {
mySubView.hidden = false
println("Before. minY:\(mySubView.frame.minY) screenheight:\(screenSize.height) subview Height:\(mySubView.frame.height)")
UIView.animateWithDuration(0.25, delay: 0.0, options: UIViewAnimationOptions.CurveEaseOut , animations:
{
self.mySubView.frame.offset(dx: 0, dy: -self.mySubView.frame.height)
}, completion: {_ in
self.subViewIsVisible = true
println("After. minY:\(self.mySubView.frame.minY) subView height:\(self.mySubView.frame.height)")
}
)
}
}
修改
mySubView的约束: (比我对Bannings的评论更容易看到)
答案 0 :(得分:0)
457和143来自你的故事板:
出现这种情况是因为viewDidLoad
方法中视图的来源不正确。虽然您对Bottom Layout Guide
有最低限制,但框架尚未调整。
因此,如果您将视图设置为500,那么您将获得如下日志:
Before. minY:500.0 screenheight:667.0 offset:167.0
After. minY:524.5 screenheight:667.0 offset:167.0
AutoLayout调用mySubView
后,您的viewDidLoad
框架将无误。