0
votes

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))
    }

}
2
You have to set the scrollview's contentSize same as UIImageView and scrollview's frame size same as UIScreen.Poles
Do you mind to use UITableview instead of UIScrollview ? Having UITableView in place, when keyboard appears automatically view scrolls up. You reeky don't have to handle all the above conditions in such case.Bharath Vankireddy
UITableView seems very hard for this simple usage, isn't it ? Poles, I tried but is still not working :(RedLens834
I think UITableView does not do the keyboard handling -- the UITableViewController does that -- so you would need to derive your VC from that.Lou Franco
Of course, but there is no other solution to move up the content when keyboard appear ?RedLens834

2 Answers

1
votes
  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.

0
votes

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.