简体   繁体   English

动态设置Scrollview的Contentsize无效

[英]Setting Scrollview's Contentsize dynamically does not work

I am desperately trying to figure this out, I've spent all day on it with no luck. 我拼命地试图解决这个问题,我整天都在这上面度过了运气。 All I want to do is have a list of textfields that a user can click on and it will place the keyboard on the page so that A) the field is not blocked and (I have figured that part out) and B) the top of the view is not cut off when the keyboard pops up. 我想要做的就是有一个文本字段列表,用户可以单击它,它将把键盘放在页面上,这样A)该字段不会被阻塞,并且(我已经弄清楚了这部分)和B)顶部弹出键盘时,视图不会被切除。 This is what I have so far ( http://imgur.com/a/LExyo ), but you can clearly see the problem ~ that's the top of the view I cannot scroll up any farther. 这就是我到目前为止的内容( http://imgur.com/a/LExyo ),但是您可以清楚地看到问题所在–这是我无法再向上滚动的视图的顶部。 I've attempted to bury the whole thing in a scrollview and then change the contentsize of the scrollview but that has done nothing. 我试图将整个东西埋在滚动视图中,然后更改滚动视图的contentsize,但是什么也没做。 Any help would be appreciated. 任何帮助,将不胜感激。 Thank you. 谢谢。 (I am trying to do this in swift 3). (我正在尝试快速完成此操作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)

Backing up Vasilii Muravev answer you have one last mistake. 备份Vasilii Muravev回答您有最后一个错误。

Step 1) 第1步)

let insets = scrollView.contentInset
//Here you should save the PREVIOUS_BOTTOM
scrollView.contentInset = UIEdgeInsets(top: insets.top, left: insets.left, bottom: /* keyboard height */, right: insets.right)

Step 2) 第2步)

let inset = scrollViewInset.contentInset
scrollView.contentInset = UIEdgeInsets(top: inset.top, left: inset.left, bottom: PREVIOUS_BOTTOM, right: inset.right)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM