swift - 移动 subview - 偏移量错误,但视觉上正确?

标签 swift

我编写了这个示例程序来尝试弄清楚发生了什么。我有一个文本字段,单击它会显示 subview 而不是显示键盘。在 subview 中,有一个关闭按钮可以移动 subview ,使顶部与屏幕底部对齐。

这样我就可以在 Storyboard 中构建 subview , subview 与屏幕的底部边缘对齐并在启动时隐藏viewDidLoad 然后调用 hideSubView 在开始之前将其移出屏幕。

我在每次轮类前后都有 println 告诉我 subview 的位置。

我遇到的问题是数字不相加。我特别告诉 swift 将 Y 偏移 210,但不知何故它只将它移动了 68.5,这 不是 屏幕底部(注意这不是通过将 subview 设置为文本字段的输入 View 来完成的).然而,当我单击文本字段时, subview 从下往上而不是某个中间点进行动画处理。谁能解释为什么会发生这两件事?另一件事是 - 初始 minY 的 457.0 来自哪里?我检查了 subview 的初始高度,奇怪的是 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 screen capture of 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 的评论更容易看到) Constraints on mySubView

最佳答案

457 和 143 来自您的 Storyboard,如下所示:

enter image description here

出现此行为是因为 viewDidLoad 方法中 View 的来源不正确。尽管您使用 Bottom Layout Guide 设置了底部约束,但是框架尚未调整。

因此,如果您将 View 的 y 设置为 500,那么您将获得如下日志:

enter image description here

Before. minY:500.0 screenheight:667.0 offset:167.0
After. minY:524.5 screenheight:667.0 offset:167.0

在 AutoLayout 调用 viewDidLoad 之后,您的 mySubView 的框架将是正确的。

关于swift - 移动 subview - 偏移量错误,但视觉上正确?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30635191/

相关文章:

iOS 核心数据 : error when assigning a value to an attribute

swift - 将自定义 key 添加到 info.plist,当我从 Swift 中的 Playground 访问时,它是零

ios - 以编程方式在特定 UICollectionViewCell 中的 TableView?

ios - 如何在 iOS 中使用 health-kit 获取 bodyTemperature?

swift - 当我将 UITextField 的输入传递给变量或 UserDefault 时,变量/UserDefault 最终为零

swift - 验证 NSTouchBar 项目的最佳实用方法

ios - 使用 Alamofire 从 Firebase 检索 JSON

ios - Swift 4.2 中带有 JSON 数据的 TableView

ios - 选择 Collection View 单元格时如何在文档目录中传递文件 URL

Swift:继承项目内的类