問(wèn)題描述
滾動(dòng)視圖
我有一個(gè) UIScrollView,帶有約束 left: 0, top: 0, right: 0, bottom: 0
I have a UIScrollView, with constraints left: 0, top: 0, right: 0, bottom: 0
內(nèi)部滾動(dòng)視圖
在這個(gè) UIScrollView
的頂部是一個(gè) UIImageView
約束 left: 0, top: 0, right: 0, height: 200
At the top of this UIScrollView
is a UIImageView
with constraints left: 0, top: 0, right: 0, height: 200
在下面我有一個(gè) UITextView
約束 left: 0, top: 0, right: 0, bottom: 0
Underneath this I have a UITextView
with constraints left: 0, top: 0, right: 0, bottom: 0
這意味著 UITextView
將根據(jù)其內(nèi)容調(diào)整大小,我為 UITextView<將
scrollingEnabled
設(shè)置為 false
/代碼>.
This means the UITextView
will resize with respect to its content, and I set the scrollingEnabled
to false
for the UITextView
.
所以,當(dāng)我跑步時(shí),它幾乎完美運(yùn)行.
So, when I run, it almost works perfectly.
其中一個(gè)問(wèn)題是 UIImageView
占用的空間比實(shí)際屏幕寬度多 10%.因此,啟用了水平滾動(dòng).
The one problem is the UIImageView
takes up about 10% more than the actual screen width. Hence, horizontal scrolling is enabled.
我已嘗試添加行
imageView.frame = CGRect(0, 0, screenSize.width, 200)
scrlView.contentSize.width = screenSize.width
但這沒(méi)有區(qū)別.我仍然可以水平滾動(dòng),并且圖像視圖仍然比實(shí)際屏幕寬度多出 10% 左右.
but this makes no difference. I can still scroll horizontally and the Image View still takes up around 10% more than the actual screen width.
注意,我沒(méi)有在情節(jié)提要中設(shè)置 imageView 屏幕寬度,僅以編程方式設(shè)置.
Note, I have not set imageView screen width in storyboard, only programatically.
有什么想法嗎?
推薦答案
這樣,
Swift 4.0
func scrollViewDidScroll(scrollView: UIScrollView) {
if scrollView.contentOffset.x>0 {
scrollView.contentOffset.x = 0
}
}
而且,你可以設(shè)置這個(gè)屬性:
And, you can set this property:
scrollImg.isDirectionalLockEnabled = true
這篇關(guān)于從 UIScrollView Swift 禁用水平滾動(dòng)的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!