我拼命地试图解决这个问题,我整天都在这上面度过了运气。我要做的只是有一个用户可以单击的文本字段列表,它将把键盘放在页面上,这样A)该字段不会被阻塞,并且(我已经弄清楚了这部分)和B)顶部弹出键盘时,视图不会被切除。这是我到目前为止的内容(http://imgur.com/a/LExyo),但是您可以清楚地看到问题所在–这是我无法进一步向上滚动的视图的顶部。我试图将整个东西埋在滚动视图中,然后更改滚动视图的contentsize,但是什么也没做。任何帮助,将不胜感激。谢谢。 (我正在尝试快速完成此操作3)。

class ViewController4: UIViewController,  UITextFieldDelegate {

    var dataState: Data?

    @IBOutlet weak var UserNameLabel: UILabel!

    @IBOutlet weak var TextField1Outlet: UITextField!


    @IBOutlet weak var TextField2Outlet: UITextField!

    @IBOutlet weak var TextField3Outlet: UITextField!


    @IBOutlet weak var TextField4Outlet: UITextField!


    @IBOutlet weak var TextField5Outlet: UITextField!

    @IBOutlet var ViewFrame: UIView!


    @IBOutlet weak var scrollView: UIScrollView!


    @IBAction func TextField1Action(_ sender: Any) {
    }

    @IBAction func TextField2Action(_ sender: Any) {
    }


    @IBAction func TextField3Action(_ sender: Any) {
    }


    @IBAction func TextField4Action(_ sender: Any) {
    }


    @IBAction func TextField5Action(_ sender: Any) {
    }


    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        UserNameLabel.text = dataState?.LoginName
    }

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if let destinationViewController = segue.destination as? ViewController1 {
            destinationViewController.dataState = dataState
        }
    }

    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        textField.resignFirstResponder()
        return true
    }

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

    override func viewDidLoad() {
        super.viewDidLoad()

        scrollView.contentSize = CGSize(width: self.view.frame.width, height: self.view.frame.height)
        scrollView.delegate = self as? UIScrollViewDelegate

        TextField1Outlet.returnKeyType = UIReturnKeyType.done
        TextField1Outlet.delegate = self

        TextField2Outlet.returnKeyType = UIReturnKeyType.done
        TextField2Outlet.delegate = self

        TextField3Outlet.returnKeyType = UIReturnKeyType.done
        TextField3Outlet.delegate = self

        TextField4Outlet.returnKeyType = UIReturnKeyType.done
        TextField4Outlet.delegate = self

        TextField5Outlet.returnKeyType = UIReturnKeyType.done
        TextField5Outlet.delegate = self

        NotificationCenter.default.addObserver(self, selector: #selector(ViewController4.keyboardWillShow), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
        NotificationCenter.default.addObserver(self, selector: #selector(ViewController4.keyboardWillHide), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
    }

    func keyboardWillShow(notification: NSNotification) {
        if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
            if self.view.frame.origin.y == 0{
                self.view.frame.origin.y -= keyboardSize.height
                let newHeight = self.view.frame.height + keyboardSize.height
                scrollView.contentSize = CGSize(width: self.view.frame.width, height: newHeight)

            }
        }
    }

    func keyboardWillHide(notification: NSNotification) {
        if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
            if self.view.frame.origin.y != 0{
                self.view.frame.origin.y += keyboardSize.height
                scrollView.contentSize = CGSize(width: self.view.frame.width, height: self.view.frame.height)
            }
        }
    }
}

最佳答案

使用内容插图而不是内容大小:

scrollView.contentInset = UIEdgeInsets(top: 0.0, left: 0.0, bottom: /* keyboard height */, right: 0.0)

关于ios - 动态设置Scrollview的Contentsize无效,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44730014/

10-10 16:41