ScrollView Add SubView to swift 3

I want to try adding a view form to the scroll, but the view is not fully added to the scroll view frame, my code is:

import UIKit

class AddIncomeVC: UIViewController {
    @IBOutlet var scrollView: UIScrollView!
    @IBOutlet var views: UIView!
    override func viewDidLoad() {
        super.viewDidLoad()
        scrollView.contentSize = views.frame.size
        scrollView.addSubview(views)

    }

    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        views.frame = CGRect(x: 0, y: 0, width: scrollView.frame.size.width, height: scrollView.frame.size.height)
    }

}

enter image description here

Thank,

+4
source share
3 answers

Change line

views.frame = CGRect(x: 0, y: 0, width: scrollView.frame.size.width, height: scrollView.frame.size.height)

to

views.frame = CGRect(x: 0, y: 0, width: scrollView.contentSize.width, height: scrollView.contentSize.height)

+2
source

I decided to solve this problem with this code:

import UIKit

class AddIncomeVC: UIViewController {
    @IBOutlet var scrollView: UIScrollView!
    @IBOutlet var views: UIView!
    override func viewDidLoad() {
        super.viewDidLoad()
        scrollView.contentSize = views.frame.size
        scrollView.addSubview(views)


    }

    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        views.frame = CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: self.view.frame.size.height)

    }

}

Thanks to everyone for giving valuable time to my question. enter image description here

+1
source

: . , - , , ... ( iPhone SE iphone 6, ).

"" , ... - iphone SE.

, ( iphone6), ( - iphone SE).

Your solution works well. After the iOS layout fully displays all the subspecies, the restrictions are updated to new screen sizes, while maintaining their proportions. In viewDidLayoutSubviews, the "view" already has the correct constraint values ​​... Thus, the view has the iphone6 ​​screen size, and you can set the frame to the correct

+1
source

Source: https://habr.com/ru/post/1664247/


All Articles