gpt4 book ai didi

swift - UIview 一直在 UIViewcontroller 的底部

转载 作者:行者123 更新时间:2023-11-28 13:10:50 25 4
gpt4 key购买 nike

我的 LoginViewController 有一个 UIView,它有 2 个 UItextfield 和 1 个 UIbutton。用户开始编写 UIView 的那一刻应该上升并为键盘留出空间。但是我的问题是当键盘消失时 UIView 没有到达它的初始位置。谁能帮帮我谢谢...(我的代码在下面)

  func keyboardON(sender: NSNotification) {

let info = sender.userInfo!
var keyboardSize = info[UIKeyboardFrameBeginUserInfoKey]!.CGRectValue().size
println("keyboard height \(keyboardSize.height)")
var frame = otherContainerView.frame
println("MainScreen :\(UIScreen.mainScreen().bounds.height)")
frame.origin.y = UIScreen.mainScreen().bounds.height - keyboardSize.height - frame.size.height
otherContainerView.frame = frame

}

func keyboardNotOn(sender: NSNotification) {
let info = sender.userInfo!
var keyboardSize = info[UIKeyboardFrameBeginUserInfoKey]!.CGRectValue().size
println("keyboard height \(keyboardSize.height)")
var frame = otherContainerView.frame
frame.origin.y = UIScreen.mainScreen().bounds.height - frame.size.height
otherContainerView.frame = frame
}

最佳答案

我建议您将此 View Controller 更改为带有静态单元格的 UITableViewController。使用 UITableViewController 时,滚动会自动处理,从而使您的问题根本不是问题。

Login page being handled with a UITableViewController.

如果我正确理解了您最初的问题,那么您正在尝试在 UIViewController 中创建登录屏幕,这很好,但比简单地创建 UITableViewController 难得多。上图是用 UITableViewController 制作的。当文本字段被选中时,它会向上滑动,当它们被取消选中时,它会返回到初始 View 。如果您切换到 UITableViewController,(即使您将 UITextFields 和按钮都放在一个单元格中),您将不需要以编程方式执行任何这些操作。 Storyboard将处理所需的更改。

import UIKit

class LoginTableViewController: UITableViewController {

@IBOutlet weak var usernameTextField : UITextField!
@IBOutlet weak var passwordTextField : UITextField!

@IBAction func login (sender: UIButton) {
//login button
}

override func viewDidLoad() {
super.viewDidLoad()
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}


override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

//If you choose to use 3 cells in your storyboard (like I've done with my login screen) you will return 3 here. If you choose to put all of the items in one cell, return 1 below.
return 3
}

}

关于swift - UIview 一直在 UIViewcontroller 的底部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31362366/

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