Scroll UITextView To Bottom

后端 未结 10 1684
误落风尘
误落风尘 2020-12-08 07:26

I am making a an app that has a UITextView and a button.

When I click the button some text will add in the UITextView.

But when c

10条回答
  •  情歌与酒
    2020-12-08 07:43

    As a generic approach for scrolling to bottom, it can be done on a UIScrollView.

    extension UIScrollView {
        func scrollToBottom() {
            let contentHeight = contentSize.height - frame.size.height
            let contentoffsetY = max(contentHeight, 0)
            setContentOffset(CGPoint(x: 0, y: contentoffsetY), animated: true)
        }
    }
    

    This will work on all descendants of UIScrollView like UITextView, UITableView etc..

提交回复
热议问题