UIScrollview doesn't work once subviews added





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















I'm new to working with ScrollViews, and I'm doing it all programatically. I must be missing something super simple, but when I have no subviews, the scrollview shows up properly and scrolls up and down. But any time I add any subviews, the whole thing refuses to show up at all.



class DetailedPostScrollView: UIScrollView {

let topLabel: UILabel = {
let label = UILabel()
label.text = "this is the top"
return label
}()

let bottomLabel: UILabel = {
let label = UILabel()
label.text = "this is the bottom"
return label
}()

override init(frame: CGRect) {
super.init(frame: frame)
contentSize = CGSize(width: frame.width, height: 2000)
alwaysBounceVertical = true

addSubviewUsingAutoLayout(topLabel, bottomLabel)
topLabel.centerXAnchor.constrain(to: self.centerXAnchor)
topLabel.widthAnchor.constrain(to: 200)
topLabel.heightAnchor.constrain(to: 50)
topLabel.topAnchor.constrain(to: self.topAnchor, with: 100)

bottomLabel.centerXAnchor.constrain(to: self.centerXAnchor)
bottomLabel.widthAnchor.constrain(to: 200)
bottomLabel.heightAnchor.constrain(to: 50)
bottomLabel.bottomAnchor.constrain(to: self.bottomAnchor)

}

required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}


}



And in my viewController, I instantiate and add the scroll view



let detailedPostScrollView = DetailedPostScrollView(frame: UIScreen.main.bounds)
detailedPostScrollView.backgroundColor = UIColor.purple
view.addSubviewUsingAutoLayout(detailedPostScrollView)


Again, I"m sure it's something super simple but I checked out all the questions and tutorial videos and couldn't see where I"m going wrong. Thanks for your help.



Edit: It seems to work fine when I do it all programmatically from a viewcontroller, as follows :



 let scrollView: UIScrollView = {
let sv = UIScrollView()
sv.contentSize = CGSize(width: view.frame.width, height: 2000)
sv.backgroundColor = UIColor.purple
return sv
}()

view.addSubviewUsingAutoLayout(scrollView)
scrollView.topAnchor.constrain(to: view.topAnchor, with: 100)
scrollView.leadingAnchor.constrain(to: view.leadingAnchor)
scrollView.trailingAnchor.constrain(to: view.trailingAnchor)
scrollView.bottomAnchor.constrain(to: view.bottomAnchor)

let topLabel: UILabel = {
let label = UILabel()
label.text = "this is the top"
return label
}()

scrollView.addSubviewUsingAutoLayout(topLabel)
topLabel.centerXAnchor.constrain(to: scrollView.centerXAnchor)
topLabel.widthAnchor.constrain(to: 200)
topLabel.heightAnchor.constrain(to: 50)
topLabel.topAnchor.constrain(to: scrollView.topAnchor)


Something is off when I create a custom scrollview and instantiate that in my vc.










share|improve this question

























  • don't forget to set constrain is active = true and set the view translatesAutoresizingMaskIntoConstraints = false

    – mazen
    Nov 27 '18 at 3:59













  • Ah sorry forgot to mention I’ve added extensions so that it’s automatically active and turns off auto resizing, that’s not the issue

    – Tin Luong
    Nov 27 '18 at 4:46


















0















I'm new to working with ScrollViews, and I'm doing it all programatically. I must be missing something super simple, but when I have no subviews, the scrollview shows up properly and scrolls up and down. But any time I add any subviews, the whole thing refuses to show up at all.



class DetailedPostScrollView: UIScrollView {

let topLabel: UILabel = {
let label = UILabel()
label.text = "this is the top"
return label
}()

let bottomLabel: UILabel = {
let label = UILabel()
label.text = "this is the bottom"
return label
}()

override init(frame: CGRect) {
super.init(frame: frame)
contentSize = CGSize(width: frame.width, height: 2000)
alwaysBounceVertical = true

addSubviewUsingAutoLayout(topLabel, bottomLabel)
topLabel.centerXAnchor.constrain(to: self.centerXAnchor)
topLabel.widthAnchor.constrain(to: 200)
topLabel.heightAnchor.constrain(to: 50)
topLabel.topAnchor.constrain(to: self.topAnchor, with: 100)

bottomLabel.centerXAnchor.constrain(to: self.centerXAnchor)
bottomLabel.widthAnchor.constrain(to: 200)
bottomLabel.heightAnchor.constrain(to: 50)
bottomLabel.bottomAnchor.constrain(to: self.bottomAnchor)

}

required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}


}



And in my viewController, I instantiate and add the scroll view



let detailedPostScrollView = DetailedPostScrollView(frame: UIScreen.main.bounds)
detailedPostScrollView.backgroundColor = UIColor.purple
view.addSubviewUsingAutoLayout(detailedPostScrollView)


Again, I"m sure it's something super simple but I checked out all the questions and tutorial videos and couldn't see where I"m going wrong. Thanks for your help.



Edit: It seems to work fine when I do it all programmatically from a viewcontroller, as follows :



 let scrollView: UIScrollView = {
let sv = UIScrollView()
sv.contentSize = CGSize(width: view.frame.width, height: 2000)
sv.backgroundColor = UIColor.purple
return sv
}()

view.addSubviewUsingAutoLayout(scrollView)
scrollView.topAnchor.constrain(to: view.topAnchor, with: 100)
scrollView.leadingAnchor.constrain(to: view.leadingAnchor)
scrollView.trailingAnchor.constrain(to: view.trailingAnchor)
scrollView.bottomAnchor.constrain(to: view.bottomAnchor)

let topLabel: UILabel = {
let label = UILabel()
label.text = "this is the top"
return label
}()

scrollView.addSubviewUsingAutoLayout(topLabel)
topLabel.centerXAnchor.constrain(to: scrollView.centerXAnchor)
topLabel.widthAnchor.constrain(to: 200)
topLabel.heightAnchor.constrain(to: 50)
topLabel.topAnchor.constrain(to: scrollView.topAnchor)


Something is off when I create a custom scrollview and instantiate that in my vc.










share|improve this question

























  • don't forget to set constrain is active = true and set the view translatesAutoresizingMaskIntoConstraints = false

    – mazen
    Nov 27 '18 at 3:59













  • Ah sorry forgot to mention I’ve added extensions so that it’s automatically active and turns off auto resizing, that’s not the issue

    – Tin Luong
    Nov 27 '18 at 4:46














0












0








0








I'm new to working with ScrollViews, and I'm doing it all programatically. I must be missing something super simple, but when I have no subviews, the scrollview shows up properly and scrolls up and down. But any time I add any subviews, the whole thing refuses to show up at all.



class DetailedPostScrollView: UIScrollView {

let topLabel: UILabel = {
let label = UILabel()
label.text = "this is the top"
return label
}()

let bottomLabel: UILabel = {
let label = UILabel()
label.text = "this is the bottom"
return label
}()

override init(frame: CGRect) {
super.init(frame: frame)
contentSize = CGSize(width: frame.width, height: 2000)
alwaysBounceVertical = true

addSubviewUsingAutoLayout(topLabel, bottomLabel)
topLabel.centerXAnchor.constrain(to: self.centerXAnchor)
topLabel.widthAnchor.constrain(to: 200)
topLabel.heightAnchor.constrain(to: 50)
topLabel.topAnchor.constrain(to: self.topAnchor, with: 100)

bottomLabel.centerXAnchor.constrain(to: self.centerXAnchor)
bottomLabel.widthAnchor.constrain(to: 200)
bottomLabel.heightAnchor.constrain(to: 50)
bottomLabel.bottomAnchor.constrain(to: self.bottomAnchor)

}

required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}


}



And in my viewController, I instantiate and add the scroll view



let detailedPostScrollView = DetailedPostScrollView(frame: UIScreen.main.bounds)
detailedPostScrollView.backgroundColor = UIColor.purple
view.addSubviewUsingAutoLayout(detailedPostScrollView)


Again, I"m sure it's something super simple but I checked out all the questions and tutorial videos and couldn't see where I"m going wrong. Thanks for your help.



Edit: It seems to work fine when I do it all programmatically from a viewcontroller, as follows :



 let scrollView: UIScrollView = {
let sv = UIScrollView()
sv.contentSize = CGSize(width: view.frame.width, height: 2000)
sv.backgroundColor = UIColor.purple
return sv
}()

view.addSubviewUsingAutoLayout(scrollView)
scrollView.topAnchor.constrain(to: view.topAnchor, with: 100)
scrollView.leadingAnchor.constrain(to: view.leadingAnchor)
scrollView.trailingAnchor.constrain(to: view.trailingAnchor)
scrollView.bottomAnchor.constrain(to: view.bottomAnchor)

let topLabel: UILabel = {
let label = UILabel()
label.text = "this is the top"
return label
}()

scrollView.addSubviewUsingAutoLayout(topLabel)
topLabel.centerXAnchor.constrain(to: scrollView.centerXAnchor)
topLabel.widthAnchor.constrain(to: 200)
topLabel.heightAnchor.constrain(to: 50)
topLabel.topAnchor.constrain(to: scrollView.topAnchor)


Something is off when I create a custom scrollview and instantiate that in my vc.










share|improve this question
















I'm new to working with ScrollViews, and I'm doing it all programatically. I must be missing something super simple, but when I have no subviews, the scrollview shows up properly and scrolls up and down. But any time I add any subviews, the whole thing refuses to show up at all.



class DetailedPostScrollView: UIScrollView {

let topLabel: UILabel = {
let label = UILabel()
label.text = "this is the top"
return label
}()

let bottomLabel: UILabel = {
let label = UILabel()
label.text = "this is the bottom"
return label
}()

override init(frame: CGRect) {
super.init(frame: frame)
contentSize = CGSize(width: frame.width, height: 2000)
alwaysBounceVertical = true

addSubviewUsingAutoLayout(topLabel, bottomLabel)
topLabel.centerXAnchor.constrain(to: self.centerXAnchor)
topLabel.widthAnchor.constrain(to: 200)
topLabel.heightAnchor.constrain(to: 50)
topLabel.topAnchor.constrain(to: self.topAnchor, with: 100)

bottomLabel.centerXAnchor.constrain(to: self.centerXAnchor)
bottomLabel.widthAnchor.constrain(to: 200)
bottomLabel.heightAnchor.constrain(to: 50)
bottomLabel.bottomAnchor.constrain(to: self.bottomAnchor)

}

required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}


}



And in my viewController, I instantiate and add the scroll view



let detailedPostScrollView = DetailedPostScrollView(frame: UIScreen.main.bounds)
detailedPostScrollView.backgroundColor = UIColor.purple
view.addSubviewUsingAutoLayout(detailedPostScrollView)


Again, I"m sure it's something super simple but I checked out all the questions and tutorial videos and couldn't see where I"m going wrong. Thanks for your help.



Edit: It seems to work fine when I do it all programmatically from a viewcontroller, as follows :



 let scrollView: UIScrollView = {
let sv = UIScrollView()
sv.contentSize = CGSize(width: view.frame.width, height: 2000)
sv.backgroundColor = UIColor.purple
return sv
}()

view.addSubviewUsingAutoLayout(scrollView)
scrollView.topAnchor.constrain(to: view.topAnchor, with: 100)
scrollView.leadingAnchor.constrain(to: view.leadingAnchor)
scrollView.trailingAnchor.constrain(to: view.trailingAnchor)
scrollView.bottomAnchor.constrain(to: view.bottomAnchor)

let topLabel: UILabel = {
let label = UILabel()
label.text = "this is the top"
return label
}()

scrollView.addSubviewUsingAutoLayout(topLabel)
topLabel.centerXAnchor.constrain(to: scrollView.centerXAnchor)
topLabel.widthAnchor.constrain(to: 200)
topLabel.heightAnchor.constrain(to: 50)
topLabel.topAnchor.constrain(to: scrollView.topAnchor)


Something is off when I create a custom scrollview and instantiate that in my vc.







swift scrollview






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 27 '18 at 0:09







Tin Luong

















asked Nov 27 '18 at 0:02









Tin LuongTin Luong

2117




2117













  • don't forget to set constrain is active = true and set the view translatesAutoresizingMaskIntoConstraints = false

    – mazen
    Nov 27 '18 at 3:59













  • Ah sorry forgot to mention I’ve added extensions so that it’s automatically active and turns off auto resizing, that’s not the issue

    – Tin Luong
    Nov 27 '18 at 4:46



















  • don't forget to set constrain is active = true and set the view translatesAutoresizingMaskIntoConstraints = false

    – mazen
    Nov 27 '18 at 3:59













  • Ah sorry forgot to mention I’ve added extensions so that it’s automatically active and turns off auto resizing, that’s not the issue

    – Tin Luong
    Nov 27 '18 at 4:46

















don't forget to set constrain is active = true and set the view translatesAutoresizingMaskIntoConstraints = false

– mazen
Nov 27 '18 at 3:59







don't forget to set constrain is active = true and set the view translatesAutoresizingMaskIntoConstraints = false

– mazen
Nov 27 '18 at 3:59















Ah sorry forgot to mention I’ve added extensions so that it’s automatically active and turns off auto resizing, that’s not the issue

– Tin Luong
Nov 27 '18 at 4:46





Ah sorry forgot to mention I’ve added extensions so that it’s automatically active and turns off auto resizing, that’s not the issue

– Tin Luong
Nov 27 '18 at 4:46












1 Answer
1






active

oldest

votes


















0














Try this :



class ViewController : UIViewController , UIScrollViewDelegate{
var scrollView : UIScrollView! ;
var containerView = UIView();
var contentSize : CGSize {
get {
return CGSize(width: view.frame.width, height: 2000);
}
}

override func viewDidLoad() {
super.viewDidLoad();
setupScrollView();
setupItems();
}

override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews();
self.scrollView.frame = self.view.bounds;
containerView.frame = CGRect(x: 0, y: 0, width: contentSize.width, height: contentSize.height);
}

private func setupScrollView() {
scrollView = UIScrollView();
scrollView.delegate = self;
scrollView.backgroundColor = .white;
scrollView.contentSize = contentSize;
self.view.addSubview(scrollView);

containerView.backgroundColor = .purple;
self.scrollView.addSubview(containerView);
}
private func setupItems() {
let topButton = UIButton();
topButton.translatesAutoresizingMaskIntoConstraints = false;
topButton.setTitle("TOP BUTTON", for: .normal);
topButton.setTitleColor(.white, for: .normal);
self.containerView.addSubview(topButton);

let bottomTextField = UITextField();
bottomTextField.translatesAutoresizingMaskIntoConstraints = false;
bottomTextField.borderStyle = .roundedRect;
bottomTextField.placeholder = "BOTTOM TEXT FIELD";
self.containerView.addSubview(bottomTextField);

topButton.centerXAnchor.constraint(equalTo: containerView.centerXAnchor).isActive = true;
topButton.topAnchor.constraint(equalTo: containerView.topAnchor, constant: 50).isActive = true;
topButton.widthAnchor.constraint(equalToConstant: 100).isActive = true;
topButton.heightAnchor.constraint(equalToConstant: 50).isActive = true;

bottomTextField.centerXAnchor.constraint(equalTo: containerView.centerXAnchor).isActive = true;
bottomTextField.bottomAnchor.constraint(equalTo: containerView.bottomAnchor, constant: -50).isActive = true;
bottomTextField.widthAnchor.constraint(equalToConstant: 200).isActive = true;
bottomTextField.heightAnchor.constraint(equalToConstant: 40).isActive = true;
}

}


Set the scroll view frame to view bound in viewDidLayoutViews instead of using the layout , and add a container view as a subview for scroll view and set the size to same content size for scroll view again in viewDidLayoutViews so when ever user rotate the phone he get the correct width , and finally add all your views to container view .






share|improve this answer
























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53490890%2fuiscrollview-doesnt-work-once-subviews-added%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    Try this :



    class ViewController : UIViewController , UIScrollViewDelegate{
    var scrollView : UIScrollView! ;
    var containerView = UIView();
    var contentSize : CGSize {
    get {
    return CGSize(width: view.frame.width, height: 2000);
    }
    }

    override func viewDidLoad() {
    super.viewDidLoad();
    setupScrollView();
    setupItems();
    }

    override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews();
    self.scrollView.frame = self.view.bounds;
    containerView.frame = CGRect(x: 0, y: 0, width: contentSize.width, height: contentSize.height);
    }

    private func setupScrollView() {
    scrollView = UIScrollView();
    scrollView.delegate = self;
    scrollView.backgroundColor = .white;
    scrollView.contentSize = contentSize;
    self.view.addSubview(scrollView);

    containerView.backgroundColor = .purple;
    self.scrollView.addSubview(containerView);
    }
    private func setupItems() {
    let topButton = UIButton();
    topButton.translatesAutoresizingMaskIntoConstraints = false;
    topButton.setTitle("TOP BUTTON", for: .normal);
    topButton.setTitleColor(.white, for: .normal);
    self.containerView.addSubview(topButton);

    let bottomTextField = UITextField();
    bottomTextField.translatesAutoresizingMaskIntoConstraints = false;
    bottomTextField.borderStyle = .roundedRect;
    bottomTextField.placeholder = "BOTTOM TEXT FIELD";
    self.containerView.addSubview(bottomTextField);

    topButton.centerXAnchor.constraint(equalTo: containerView.centerXAnchor).isActive = true;
    topButton.topAnchor.constraint(equalTo: containerView.topAnchor, constant: 50).isActive = true;
    topButton.widthAnchor.constraint(equalToConstant: 100).isActive = true;
    topButton.heightAnchor.constraint(equalToConstant: 50).isActive = true;

    bottomTextField.centerXAnchor.constraint(equalTo: containerView.centerXAnchor).isActive = true;
    bottomTextField.bottomAnchor.constraint(equalTo: containerView.bottomAnchor, constant: -50).isActive = true;
    bottomTextField.widthAnchor.constraint(equalToConstant: 200).isActive = true;
    bottomTextField.heightAnchor.constraint(equalToConstant: 40).isActive = true;
    }

    }


    Set the scroll view frame to view bound in viewDidLayoutViews instead of using the layout , and add a container view as a subview for scroll view and set the size to same content size for scroll view again in viewDidLayoutViews so when ever user rotate the phone he get the correct width , and finally add all your views to container view .






    share|improve this answer




























      0














      Try this :



      class ViewController : UIViewController , UIScrollViewDelegate{
      var scrollView : UIScrollView! ;
      var containerView = UIView();
      var contentSize : CGSize {
      get {
      return CGSize(width: view.frame.width, height: 2000);
      }
      }

      override func viewDidLoad() {
      super.viewDidLoad();
      setupScrollView();
      setupItems();
      }

      override func viewDidLayoutSubviews() {
      super.viewDidLayoutSubviews();
      self.scrollView.frame = self.view.bounds;
      containerView.frame = CGRect(x: 0, y: 0, width: contentSize.width, height: contentSize.height);
      }

      private func setupScrollView() {
      scrollView = UIScrollView();
      scrollView.delegate = self;
      scrollView.backgroundColor = .white;
      scrollView.contentSize = contentSize;
      self.view.addSubview(scrollView);

      containerView.backgroundColor = .purple;
      self.scrollView.addSubview(containerView);
      }
      private func setupItems() {
      let topButton = UIButton();
      topButton.translatesAutoresizingMaskIntoConstraints = false;
      topButton.setTitle("TOP BUTTON", for: .normal);
      topButton.setTitleColor(.white, for: .normal);
      self.containerView.addSubview(topButton);

      let bottomTextField = UITextField();
      bottomTextField.translatesAutoresizingMaskIntoConstraints = false;
      bottomTextField.borderStyle = .roundedRect;
      bottomTextField.placeholder = "BOTTOM TEXT FIELD";
      self.containerView.addSubview(bottomTextField);

      topButton.centerXAnchor.constraint(equalTo: containerView.centerXAnchor).isActive = true;
      topButton.topAnchor.constraint(equalTo: containerView.topAnchor, constant: 50).isActive = true;
      topButton.widthAnchor.constraint(equalToConstant: 100).isActive = true;
      topButton.heightAnchor.constraint(equalToConstant: 50).isActive = true;

      bottomTextField.centerXAnchor.constraint(equalTo: containerView.centerXAnchor).isActive = true;
      bottomTextField.bottomAnchor.constraint(equalTo: containerView.bottomAnchor, constant: -50).isActive = true;
      bottomTextField.widthAnchor.constraint(equalToConstant: 200).isActive = true;
      bottomTextField.heightAnchor.constraint(equalToConstant: 40).isActive = true;
      }

      }


      Set the scroll view frame to view bound in viewDidLayoutViews instead of using the layout , and add a container view as a subview for scroll view and set the size to same content size for scroll view again in viewDidLayoutViews so when ever user rotate the phone he get the correct width , and finally add all your views to container view .






      share|improve this answer


























        0












        0








        0







        Try this :



        class ViewController : UIViewController , UIScrollViewDelegate{
        var scrollView : UIScrollView! ;
        var containerView = UIView();
        var contentSize : CGSize {
        get {
        return CGSize(width: view.frame.width, height: 2000);
        }
        }

        override func viewDidLoad() {
        super.viewDidLoad();
        setupScrollView();
        setupItems();
        }

        override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews();
        self.scrollView.frame = self.view.bounds;
        containerView.frame = CGRect(x: 0, y: 0, width: contentSize.width, height: contentSize.height);
        }

        private func setupScrollView() {
        scrollView = UIScrollView();
        scrollView.delegate = self;
        scrollView.backgroundColor = .white;
        scrollView.contentSize = contentSize;
        self.view.addSubview(scrollView);

        containerView.backgroundColor = .purple;
        self.scrollView.addSubview(containerView);
        }
        private func setupItems() {
        let topButton = UIButton();
        topButton.translatesAutoresizingMaskIntoConstraints = false;
        topButton.setTitle("TOP BUTTON", for: .normal);
        topButton.setTitleColor(.white, for: .normal);
        self.containerView.addSubview(topButton);

        let bottomTextField = UITextField();
        bottomTextField.translatesAutoresizingMaskIntoConstraints = false;
        bottomTextField.borderStyle = .roundedRect;
        bottomTextField.placeholder = "BOTTOM TEXT FIELD";
        self.containerView.addSubview(bottomTextField);

        topButton.centerXAnchor.constraint(equalTo: containerView.centerXAnchor).isActive = true;
        topButton.topAnchor.constraint(equalTo: containerView.topAnchor, constant: 50).isActive = true;
        topButton.widthAnchor.constraint(equalToConstant: 100).isActive = true;
        topButton.heightAnchor.constraint(equalToConstant: 50).isActive = true;

        bottomTextField.centerXAnchor.constraint(equalTo: containerView.centerXAnchor).isActive = true;
        bottomTextField.bottomAnchor.constraint(equalTo: containerView.bottomAnchor, constant: -50).isActive = true;
        bottomTextField.widthAnchor.constraint(equalToConstant: 200).isActive = true;
        bottomTextField.heightAnchor.constraint(equalToConstant: 40).isActive = true;
        }

        }


        Set the scroll view frame to view bound in viewDidLayoutViews instead of using the layout , and add a container view as a subview for scroll view and set the size to same content size for scroll view again in viewDidLayoutViews so when ever user rotate the phone he get the correct width , and finally add all your views to container view .






        share|improve this answer













        Try this :



        class ViewController : UIViewController , UIScrollViewDelegate{
        var scrollView : UIScrollView! ;
        var containerView = UIView();
        var contentSize : CGSize {
        get {
        return CGSize(width: view.frame.width, height: 2000);
        }
        }

        override func viewDidLoad() {
        super.viewDidLoad();
        setupScrollView();
        setupItems();
        }

        override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews();
        self.scrollView.frame = self.view.bounds;
        containerView.frame = CGRect(x: 0, y: 0, width: contentSize.width, height: contentSize.height);
        }

        private func setupScrollView() {
        scrollView = UIScrollView();
        scrollView.delegate = self;
        scrollView.backgroundColor = .white;
        scrollView.contentSize = contentSize;
        self.view.addSubview(scrollView);

        containerView.backgroundColor = .purple;
        self.scrollView.addSubview(containerView);
        }
        private func setupItems() {
        let topButton = UIButton();
        topButton.translatesAutoresizingMaskIntoConstraints = false;
        topButton.setTitle("TOP BUTTON", for: .normal);
        topButton.setTitleColor(.white, for: .normal);
        self.containerView.addSubview(topButton);

        let bottomTextField = UITextField();
        bottomTextField.translatesAutoresizingMaskIntoConstraints = false;
        bottomTextField.borderStyle = .roundedRect;
        bottomTextField.placeholder = "BOTTOM TEXT FIELD";
        self.containerView.addSubview(bottomTextField);

        topButton.centerXAnchor.constraint(equalTo: containerView.centerXAnchor).isActive = true;
        topButton.topAnchor.constraint(equalTo: containerView.topAnchor, constant: 50).isActive = true;
        topButton.widthAnchor.constraint(equalToConstant: 100).isActive = true;
        topButton.heightAnchor.constraint(equalToConstant: 50).isActive = true;

        bottomTextField.centerXAnchor.constraint(equalTo: containerView.centerXAnchor).isActive = true;
        bottomTextField.bottomAnchor.constraint(equalTo: containerView.bottomAnchor, constant: -50).isActive = true;
        bottomTextField.widthAnchor.constraint(equalToConstant: 200).isActive = true;
        bottomTextField.heightAnchor.constraint(equalToConstant: 40).isActive = true;
        }

        }


        Set the scroll view frame to view bound in viewDidLayoutViews instead of using the layout , and add a container view as a subview for scroll view and set the size to same content size for scroll view again in viewDidLayoutViews so when ever user rotate the phone he get the correct width , and finally add all your views to container view .







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 27 '18 at 6:41









        mazenmazen

        1238




        1238
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53490890%2fuiscrollview-doesnt-work-once-subviews-added%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            To store a contact into the json file from server.js file using a class in NodeJS

            Redirect URL with Chrome Remote Debugging Android Devices

            Dieringhausen