UIScrollView not scrolling in Swift

前端 未结 12 1053
Happy的楠姐
Happy的楠姐 2021-02-03 19:03

My UIScrollView won\'t scroll down. I don\'t know why. I already followed Apple documentation regarding to this issue.

@IBOutlet weak var scroller: UIScrollView!         


        
相关标签:
12条回答
  • 2021-02-03 19:18

    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
    
    0 讨论(0)
  • 2021-02-03 19:20

    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.

    0 讨论(0)
  • 2021-02-03 19:25

    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)
    
    }
    
    0 讨论(0)
  • 2021-02-03 19:27

    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

    0 讨论(0)
  • 2021-02-03 19:27

    FWIW, I found that I needed to use sathish's solution from above, though it was insufficient to effect the intervention in viewDidAppear alone. I had to instead make the adjustment for every new content assignment:

    func display(pattern: Pattern) {
        let text : NSAttributedString = pattern.body()
        patternTextView.attributedText = text
    
        // Set the size of the view. Autolayout seems to get in the way of
        // a correct size calculation
        patternTextView.contentSize = CGSize(width: 348, height: 620)
    }
    

    The manifest constants (yeah, I know, yuk, but it makes it easier to understand here) are from the autolayout spec.

    0 讨论(0)
  • 2021-02-03 19:29

    It worked for me. In Size Inspector Layout = Translates Mask into constraints. Autoresizing = all click.

    0 讨论(0)
提交回复
热议问题