可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
My UIScrollView wont scroll down. I dont know why. I already followed apple documentation regarding to this issue.
@IBOutlet weak var scroller: UIScrollView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func viewDidLayoutSubviews() {
scroller.scrollEnabled = true
// Do any additional setup after loading the view
scroller.contentSize = CGSizeMake(400, 2300)
}
Please advice. Thank you.
回答1:
You need to set the frame
of your UIScrollView
so that it is less than the contentsize
. Otherwise, it won't scroll.
Also, I would recommend that you add scroller.contentSize = CGSizeMake(400, 2300)
to your viewDidLoad
method.
回答2:
Alot of the time the code is correct if you have followed a tutorial but what many beginners do not know is that the scrollView is NOT going to scroll normally through the simulator. It is suppose to scroll only when you press down on the mousepad and simultaneously scroll. Many Experienced XCode/Swift/Obj-C users are so use to doing this and so they do not know how it could possibly be overlooked by beginners. Ciao :-)
@IBOutlet weak var scrollView: UIScrollView!
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(scrollView)
// Do any additional setup after the view
}
override func viewWillLayoutSubviews(){
super.viewWillLayoutSubviews()
scrollView.contentSize = CGSize(width: 375, height: 800)
}
This code will work perfectly fine as long as you do what I said up above
回答3:
Swift 3.0 version
scroller.contentSize = CGSize(width: scroller.contentSize.width, height: 2000)
回答4:
If you are using autolayout, then the contentSize property stops working and it will try to infer the content size from the constraints. If that is the case, then your problem could be that you are not defining the necessary constraints to the content view so that the scrollview can infer the content size.
You should define the constraints of your content view to the top and bottom edges of the scrollview.
回答5:
I do not know it is a good solution, but you can try to set headerview to empty UITableView.
let scrollView: UIView = UIView(frame: CGRectMake(0, 0, 400, 2300))
tableView.tableHeaderView = scrollView
回答6:
If you use AutoLayout,
Set content size in viewdidAppear which works for me.
override func viewDidAppear(_ animated: Bool) {
scrollView.contentSize = CGSize(width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height+300)
}
回答7:
The view might not seem to move down for two reasons as your code is perfectly sound.
- First reason, you don't see the scroll indicator
- Second reason, theres nothing in the view to display
The second reason is not enough to say your view isn't scrolling but if you do not see the scroll indicator, then you might believe the view does not scroll when it in fact does.
The first reason I see happening when the constraints are not set in the storyboard especially if the view controller size in the storyboard is set to "Any". The scroll view would be wider then the screen of the simulated or real device and thus the scroll indicator would be outside of the visible screen.
回答8:
I've had problems with this, and couldn't find the answer for me until I worked it out. I had a scroll view inside of a normal view and as soon as I replaced the 'view' with a 'scroll view' it scrolled fine. Just incase anyone stumbles on that.
I'm not sure if thats meant to stop it from working as I'm very inexperienced but it seemed to be my solution. Hope it helps someone.
回答9:
In my case, I used UIStackView inside UIScrollView.
Added some views-elements from code to stackview.
It won't scroll.
Fixed it by setting stackview's userInteractionEnabled
to false
.
回答10:
The problem could be that your scrollView doesn't know its contentSize like stated above, but the fix is easier than what the above answers are. Like Carlos said but I will elaborate more. If you want your scrollView to scroll vertically(up & down), make your contentView which is in the hierarchy of the scrollView equal width to the ViewController and give it a height constraint that works for your project i.e. 700. For the opposite(horizontally) make the height equal to the ViewController and the width some big number that works for your project.