Home>

I'm trying to achieve scrolling vertically using scrollview.
In order to make it easier to add content restrictions, I'm going to make one view in the scroll view and put the elements I want to place in it.
Reference URL

The code in ViewController is as follows.

override func viewDidLoad () {
        self.view.translatesAutoresizingMaskIntoConstraints = false
        let scrollView = UIScrollView ()
        scrollView.translatesAutoresizingMaskIntoConstraints = false
        let baseView = UIView ()
        baseView.translatesAutoresizingMaskIntoConstraints = false
        self.view.addSubview (scrollView)
        self.view.addSubview (baseView)
        scrollView.topAnchor.constraint (equalTo: self.view.topAnchor) .isActive = true
        scrollView.bottomAnchor.constraint (equalTo: self.view.bottomAnchor) .isActive = true
        scrollView.leftAnchor.constraint (equalTo: self.view.leftAnchor) .isActive = true
        scrollView.rightAnchor.constraint (equalTo: self.view.rightAnchor) .isActive = true
        scrollView.widthAnchor.constraint (equalTo: self.view.widthAnchor) .isActive = true
        baseView.topAnchor.constraint (equalTo: scrollView.topAnchor) .isActive = true
        baseView.bottomAnchor.constraint (equalTo: scrollView.bottomAnchor) .isActive = true
        baseView.leftAnchor.constraint (equalTo: scrollView.leftAnchor) .isActive = true
        baseView.rightAnchor.constraint (equalTo: scrollView.rightAnchor) .isActive = true
        baseView.widthAnchor.constraint (equalTo: scrollView.widthAnchor) .isActive = true
}

It seems to be working so far, but in order to actually realize vertical scrolling, if you specify a height that is higher than the screen height forbaseView, an error will be displayed.

Code I want to add ↓

baseView.heightAnchor.constraint (equalToConstant: 2000) .isActive = true


Error details ↓

2018-08-31 14: 30: 06.164994 + 0900 SamplApp [91756: 17718038] [LayoutConstraints] Unable to simultaneously satisfy constraints.
    Probably at least one of the constraints in the following list is one you don't want.
    Try this:
        (1) look at each constraint and try to figure out which you don't expect;
        (2) find the code that added the unwanted constraint or constraints and fix it.
    (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints)
(
    "<NSAutoresizingMaskLayoutConstraint: 0x60400008ccb0 h =-&v =-&UIView: 0x7fb1e7a0ee80.height == 1080 (active)>",
    "<NSLayoutConstraint: 0x60000008e470 UIView: 0x7fb1e7a12600.height == 2260 (active)>",
    "<NSLayoutConstraint: 0x60000008c990 V: |-(0)-[UIScrollView: 0x7fb1e881ba00] (active, names: '|': UIView: 0x7fb1e7a14a10)>",
    "<NSLayoutConstraint: 0x60000008d1b0 UIScrollView: 0x7fb1e881ba00.bottom == UIView: 0x7fb1e7a14a10.bottom (active)>",
    "<NSLayoutConstraint: 0x60000008d660 UIView: 0x7fb1e7a12600.top == UIScrollView: 0x7fb1e881ba00.top (active)>",
    "<NSLayoutConstraint: 0x60000008d750 UIView: 0x7fb1e7a12600.bottom == UIScrollView: 0x7fb1e881ba00.bottom (active)>",
    "<NSLayoutConstraint: 0x600000090770 V: |-(0)-[UIView: 0x7fb1e7a14a10] (active, names: '|': UIView: 0x7fb1e7a0ee80)>",
    "<NSLayoutConstraint: 0x6000000907c0 V: [UIView: 0x7fb1e7a14a10]-(0)-| (active, names: '|': UIView: 0x7fb1e7a0ee80)>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint: 0x60000008e470 UIView: 0x7fb1e7a12600.height == 2000 (active)>


I checked and found that onlytranslatesAutoresizingMaskIntoConstraintswas set tofalse.
Can you tell me why i am competing?

By the way, if you do the same thing on the storyboard, it will scroll without problems.

  • Answer # 1

    I'm sorry, I solved it myself.
    It was because the view that should be added as a child of scrollView was added to self.view.

    Modified code

    override func viewDidLoad () {
            self.view.translatesAutoresizingMaskIntoConstraints = false
            let scrollView = UIScrollView ()
            scrollView.translatesAutoresizingMaskIntoConstraints = false
            let baseView = UIView ()
            baseView.translatesAutoresizingMaskIntoConstraints = false
            self.view.addSubview (scrollView)
            scrollView.addSubview (baseView) // modify here
            scrollView.topAnchor.constraint (equalTo: self.view.topAnchor) .isActive = true
            scrollView.bottomAnchor.constraint (equalTo: self.view.bottomAnchor) .isActive = true
            scrollView.leftAnchor.constraint (equalTo: self.view.leftAnchor) .isActive = true
            scrollView.rightAnchor.constraint (equalTo: self.view.rightAnchor) .isActive = true
            scrollView.widthAnchor.constraint (equalTo: self.view.widthAnchor) .isActive = true
            baseView.topAnchor.constraint (equalTo: scrollView.topAnchor) .isActive = true
            baseView.bottomAnchor.constraint (equalTo: scrollView.bottomAnchor) .isActive = true
            baseView.leftAnchor.constraint (equalTo: scrollView.leftAnchor) .isActive = true
            baseView.rightAnchor.constraint (equalTo: scrollView.rightAnchor) .isActive = true
            baseView.widthAnchor.constraint (equalTo: scrollView.widthAnchor) .isActive = true
    }

  • Answer # 2

    For now,

    scrollView.addSubview (baseView)
    Isn't it

    ?