Unable to set the width of scroll View iOS Swift

I am unable to set the width of the scrollView to the width of the screen. Could someone please guide where I am going wrong ?

enter image description here

  • iOS - Concurrent access to memory resources
  • Check if property is set in Core Data?
  • Can AVFoundation be coerced into playing a local .ts file?
  • WorldNode conflicting with object spawning
  • canOpenURL not working in ios 10
  • Can't open particle SKS files
  • Here is how I have enabled a scroll view

    var scrollView = UIScrollView()
    var contentView = UIView()
    override func viewDidLoad() {
        screenHeight    = screenSize.height
        screenWidth     = screenSize.width
        // SCROLL VIEW
        scrollView = UIScrollView(frame: view.bounds)
       // Here I am adding all my labels and textview fields to content view
        // Adding content view to the scrollView
        // Pinning the contentView to the scroll view
        pinView(contentView, to: scrollView)
        // Here I am Setting the constraints for all the items in the contentView

    // The below functions are used to pin one view to another view

       public func pinView(_ view: UIView, to scrollView: UIScrollView) {
            view.translatesAutoresizingMaskIntoConstraints = false
            view.pin(to: scrollView)
        public func pin(to view: UIView) {
                leadingAnchor.constraint(equalTo: view.leadingAnchor),
                trailingAnchor.constraint(equalTo: view.trailingAnchor),
                topAnchor.constraint(equalTo: view.topAnchor),
                bottomAnchor.constraint(equalTo: view.bottomAnchor)

    9 Solutions Collect From Internet About “Unable to set the width of scroll View iOS Swift”

    Use below methods for get exact size of screen accordingly iPhone.
    Hope using this method, your problem solved. If you have doubt Please comment here.

    //MARK: - Main Screen Width
    func getScreenWidth() -> CGFloat{
        return UIScreen.main.bounds.size.width
    //MARK: - Main Screen Height
    func getScreenHeight() -> CGFloat{
        return UIScreen.main.bounds.height

    viewDidLoad load your ui from storyboard or nib. And size depend on what you select in Interface Builder.

    You can initialize ui in viewWillAppear and add a variable isInitialized .

    override func viewWillAppear(_ animated: Bool) {
        if !isInitialized {
            isInitialized = true
            // do your stuff

    Scroll.contentSize = CGSize(width: self.view.frame.size.width, height: self.view.frame.size.height)

    make one subview horizontally center (using autolayout constraint) to a scrollview in interface builder or by programmatically

    Ok so there are a couple of ways you can fix this:

    1.- in the storyboard go to your view and you’re going to hold the left click in your scroll view and release it over the main view, then you’ll click equal width

    2.- in the view did load put

    self.scrollViewName.frame.size.width = self.view.frame.width

    You can set the contentInset on the scrollView, and it will adjust the content size of the scrolling area within the scrollView, here is a quick example

    scrollView.contentInset UIEdgeInsets(top: 0, left: 0, bottom: 0, right: padding)

    That should do the trick 🙂

    As you are setting constraints from viewDidLoad, but view is not properly layout in this method. You should set constraint when lay out of views is done. So you should set constraints in :

    override func viewDidLayoutSubviews() {
    // set constraints here

    And you can use a variable to check if constraints are already added.

    Try following code, may be you are not able to change frame because of autoresizing, So try to write following code in viewDidAppear method

    override func viewDidAppear(_ animated: Bool) {
       // Set the frame of scrollview
       self.scrollViewName.frame = CGRectMake(self.scrollViewName.frame.origin.x, 
       self.scrollViewName.frame.origin.y, self.view.frame.size.width, self.view.frame.size.height);

    Pin your scrollView to view instead of setting its frame.

    Add a constraint that sets the width of the contentView equal to the width of the scrollView:

    contentView.widthAnchor.constraint(equalTo: scrollView.widthAnchor).isActive = true