2015-07-11 56 views
0

我的LoginViewController有一个UIView有2个UItextfield和1个UIbutton。用户开始写UIView的时刻应该上去,为键盘留出空间。但是,我的问题是,当键盘消失UIView没有在其初始位置。谁能帮我请谢谢...(我的代码如下)UIview继续在UIViewcontroller的底部

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 
} 

回答

2

我建议你改变这种看法控制器与静态细胞的UITableViewController。当使用UITableViewController时,滚动会自动处理,从而使问题完全没有问题。

Login page being handled with a UITableViewController.

如果我正确理解您最初的问题,你想创造一个UIViewController,这是很好的一个登录界面,但更难那将是比简单地创建一个UITableViewController。上面的图片是用UITableViewController制作的。当文本字段被选中时,它向上滑动,当它们被取消选择时,它将回到它的初始视图。如果切换到UITableViewController(即使将UITextFields和按钮放在一个单元中),也不需要以编程方式执行任何操作。故事板将处理所需的更改。

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 
    } 

} 
+0

我已经更新了我的回复,以更多地解释为什么您尝试实施的方法比它可能更难。 –

+0

在您的main.storyboard中,您需要拖出一个UITableViewController并将其设置为LoginTableViewController类型。一旦你这样做,确保细胞是静态的。添加你的UITextFields和你的UIButton。然后将您的文本框和按钮连接到上面找到的IBOutlets。这就是你应该做的。 –

+0

如果您的单元格是动态的,则只需设置cellForRowAtIndexPath。但是,你会希望它们是静态的。 –