xcode 我的 ScrollView 没有垂直滚动
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/48640840/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
My ScrollView is not scrolling vertically
提问by user616076
I'm writing an app in Xcode 9 with Swift 4 and I've added a UIScrollview to a view which is intended to show a jPeg which is 3030 pixels in height. I've added the scrollview to my view and assigned the delegate in Outlets in IB. I've attached the IBOutlet called scrollView to the UIScrollview and added a UIImageView with the jPeg to the UIScrollview. I've set the size of the UIScrollView to 375W and 620H and then set the UIImageView to 375W and 3030H. this should complete the work in Interface builder.
我正在使用 Swift 4 在 Xcode 9 中编写一个应用程序,并且我已经将一个 UIScrollview 添加到一个视图中,该视图旨在显示一个高度为 3030 像素的 jPeg。我已将滚动视图添加到我的视图中,并在 IB 的 Outlets 中分配了委托。我已将名为 scrollView 的 IBOutlet 附加到 UIScrollview,并将带有 jPeg 的 UIImageView 添加到 UIScrollview。我将 UIScrollView 的大小设置为 375W 和 620H,然后将 UIImageView 设置为 375W 和 3030H。这应该完成界面构建器中的工作。
In the Controller I've added UIScrollViewDelegate to the Class and added the code below to ViewDidLoad
在控制器中,我将 UIScrollViewDelegate 添加到类并将下面的代码添加到 ViewDidLoad
//scrollView.delegate = self
scrollView.contentSize = CGSize(width: 375, height: 3040)
I've commented out the delegate line as I've done that in IB. When I run the app, the screen comes upon with the image but when I try to scroll it barely scrolls more than one screen. What have I missed?
我已经注释掉了委托行,就像我在 IB 中所做的那样。当我运行该应用程序时,屏幕上出现了图像,但是当我尝试滚动时,它几乎不会滚动多个屏幕。我错过了什么?
回答by iOS Team
You need to add a UIViewinside your scrollview and then add that view top/bottom/leading/trailing 0 to scrollview then add your image view inside view and add image view bottom constraint to that view, So your scroll view will work properly.
您需要在滚动视图中添加一个UIView,然后将该视图顶部/底部/前导/尾随 0 添加到滚动视图,然后在视图内部添加图像视图并将图像视图底部约束添加到该视图,这样您的滚动视图将正常工作。
Please refer below image for more details.
请参阅下图了解更多详情。
回答by Suganya
You have to place your scroll view inside a UIView and another UIView inside your scroll view.
您必须将滚动视图放在一个 UIView 中,并将另一个 UIView 放在滚动视图中。
- Outer view Constraints: Leading,trailing,top and bottom - 0
- Scroll view Constraints: Leading,trailing,top and bottom - 0
- Inner view Constraints: Leading,trailling and top to Superview, Equal width to Outer view , Bottom space to -250 and Height equals 900(Height of your content)
- Set your contentsize now.
- 外部视图约束:前导、尾随、顶部和底部 - 0
- 滚动视图约束:前导、尾随、顶部和底部 - 0
- 内部视图约束:前导、拖尾和顶部到超级视图,与外部视图等宽,底部空间为 -250,高度等于 900(内容的高度)
- 现在设置你的内容大小。
回答by Nischal Hada
You can implement as below image screen shots
您可以实现如下图像屏幕截图
//Height Constant of image
@IBOutlet weak var ConstHeight: NSLayoutConstraint!
override func viewDidLoad() {
super.viewDidLoad()
if #available(iOS 11.0, *) {
self.scrollView.contentInsetAdjustmentBehavior = .never
}else{
self.automaticallyAdjustsScrollViewInsets = false
}
//You can change height on here too
self.ConstHeight.constant = 800
}