RedLens834
RedLens834

Reputation: 231

UIScrollView does not fit with constraints

I'd like to use a scrollView to move the nested view content up when the keyboard appears. (Maybe you know a better solution ?)

So, I put a UIScrollView into my UIViewController and a UIImageView into my UIScrollView. The problem is my UIScrollView is as large as my image size despite constraints.

I put the following constraints :

scrollView.addConstraintsWithFormat(format: "H:|[v0]|", views: backgroundImage)
scrollView.addConstraintsWithFormat(format: "V:|[v0]|", views: backgroundImage)

self.view.addConstraintsWithFormat(format: "H:|[v0]|", views: scrollView)
self.view.addConstraintsWithFormat(format: "V:|[v0]|", views: scrollView) 

Someone have a solution ?

This is my full UIViewController code :

import UIKit

class HomeViewController: UIViewController {

    let scrollView: UIScrollView = {
        let screenSize = UIScreen.main.bounds
        let scrollView = UIScrollView()
        scrollView.backgroundColor = .red
        scrollView.contentSize = CGSize(width: screenSize.width, height: screenSize.height)
        return scrollView
    }()

    let backgroundImage:  UIImageView = {
        let imageView = UIImageView()
        imageView.image = UIImage(named: "BACKGROUND_ASIA")
        imageView.alpha = 0.5
        return imageView
    }()

    override func viewDidLoad() {
        setupHomeView()
        super.viewDidLoad()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

    func setupHomeView() {
        self.view.backgroundColor = UIColor.black

        self.view.addSubview(scrollView)
        self.view.addConstraintsWithFormat(format: "H:|[v0]|", views: scrollView)
        self.view.addConstraintsWithFormat(format: "V:|[v0]|", views: scrollView)

        scrollView.addSubview(backgroundImage)
        scrollView.addConstraintsWithFormat(format: "H:|[v0]|", views: backgroundImage)
        scrollView.addConstraintsWithFormat(format: "V:|[v0]|", views: backgroundImage)
        }
}

extension UIView {

    func addConstraintsWithFormat(format: String, views: UIView...) {
        var viewsDictionary = [String: UIView]()
        for (index, view) in views.enumerated() {
            let key = "v\(index)"
            viewsDictionary[key] = view
            view.translatesAutoresizingMaskIntoConstraints = false
        }

        addConstraints(NSLayoutConstraint.constraints(withVisualFormat: format, options: NSLayoutFormatOptions(), metrics: nil, views: viewsDictionary))
    }

}

Upvotes: 0

Views: 225

Answers (2)

RedLens834
RedLens834

Reputation: 231

Thanks for your answer PEEJWEEJ, but I found another alternative to my problem. I used the NotificationCenter to notify keyboard opening and I made a view.animate() to scroll my view. By this way I avoid to use a scrollView or a tableView.

Upvotes: 0

GetSwifty
GetSwifty

Reputation: 7746

  1. You should call super first in viewDidLoad.
  2. You should read up on how scrollViews work.

Here's what you need: The ScrollView needs constraints for left/right/top/bottom. This will determine the size of the presentable portion of the scrollview. This is the part that you would resize when the keyboard shows.

Then, you need to set the size of the ScrollView's content. This is the content that can be scrolled. You will need to manually set the size of your imageView, or setup equality between your imageView and views that exist outside of your scrollview. (eg imageView.width == view.width).

Hope this points in the right direction. You might want to consider using Interface Builder to set this up so you can see all the constraints and get warning when things aren't set up properly.

Upvotes: 1

Related Questions