Autolayout ScrollView programmatically swift











up vote
0
down vote

favorite












I need to set the layout of a ScrollView full-screen with autolayout. at the moment I have this code but not the full-height sect and I can not understand which variable to change. I have this code



func setControlsType(controlsType: ControlsType, bounds: CGRect, startingDim: CGFloat, scrolledDim: CGFloat) {
self.controlsType = controlsType
if Utils.isInPortraitState() {
/*self.startingFrame = CGRect(x: 0, y: bounds.height - startingDim, width: bounds.width, height: scrolledDim)
self.scrolledFrame = CGRect(x: 0, y: bounds.origin.y + bounds.height - scrolledDim, width: bounds.width, height: scrolledDim)*/
self.startingFrame = CGRect(x: 0, y: bounds.height, width: bounds.width, height: scrolledDim)
self.scrolledFrame = CGRect(x: 0, y: bounds.origin.y + bounds.height, width: bounds.width, height: scrolledDim)
} else {
self.startingFrame = CGRect(x: startingDim - scrolledDim, y: 0, width: scrolledDim, height: bounds.height)
self.scrolledFrame = CGRect(x: 0, y: 0, width: scrolledDim, height: bounds.height)
}


func setControls(type: ControlsType) {
let safeBounds = SafeAreaManager.letsDoIt(view: self, upon: .bounds)

/*let sDim = (Utils.isInPortraitState() ? safeBounds.height + (self.workbenchView.frame.origin.y + self.workbenchView.frame.size.height) : safeBounds.width + (self.workbenchView.frame.size.width + self.workbenchView.frame.width))*/

let sDim = CGFloat(0)

var scDim = CGFloat(0)

if Utils.isIPad() {
if Utils.isInPortraitState() {
scDim = safeBounds.height - (self.workbenchView.frame.origin.y + 2 * (self.workbenchView.frame.size.height/3))
} else {
scDim = self.workbenchView.frame.origin.x + self.workbenchView.frame.size.width/3
}
} else {
if Utils.isInPortraitState() {
scDim = safeBounds.height - self.workbenchView.frame.size.height
} else {
scDim = safeBounds.width - self.workbenchView.frame.size.width
}
}
self.controlsView.setControlsType(controlsType: type, bounds: safeBounds, startingDim: sDim, scrolledDim: scDim)
}









share|improve this question
























  • Why not use constraints?
    – elbert rivas
    Nov 7 at 10:02










  • I tried to use them, but these functions are recalled from other parts of the code and do not accept them. I do not know how to solve
    – Stefano
    Nov 7 at 10:31










  • I don't really understand what do you want to accomplish. Do you want the UIScrollView to take the full screen of your iDevice or just have the same bounds as your blue rectangular view at the bottom of your screen?
    – YoanGJ
    Nov 7 at 12:28










  • @YoanGJ sorry. I want take my scrollview to the full screen with this code, but I can't understand frame propriety.. help please
    – Stefano
    Nov 7 at 12:38















up vote
0
down vote

favorite












I need to set the layout of a ScrollView full-screen with autolayout. at the moment I have this code but not the full-height sect and I can not understand which variable to change. I have this code



func setControlsType(controlsType: ControlsType, bounds: CGRect, startingDim: CGFloat, scrolledDim: CGFloat) {
self.controlsType = controlsType
if Utils.isInPortraitState() {
/*self.startingFrame = CGRect(x: 0, y: bounds.height - startingDim, width: bounds.width, height: scrolledDim)
self.scrolledFrame = CGRect(x: 0, y: bounds.origin.y + bounds.height - scrolledDim, width: bounds.width, height: scrolledDim)*/
self.startingFrame = CGRect(x: 0, y: bounds.height, width: bounds.width, height: scrolledDim)
self.scrolledFrame = CGRect(x: 0, y: bounds.origin.y + bounds.height, width: bounds.width, height: scrolledDim)
} else {
self.startingFrame = CGRect(x: startingDim - scrolledDim, y: 0, width: scrolledDim, height: bounds.height)
self.scrolledFrame = CGRect(x: 0, y: 0, width: scrolledDim, height: bounds.height)
}


func setControls(type: ControlsType) {
let safeBounds = SafeAreaManager.letsDoIt(view: self, upon: .bounds)

/*let sDim = (Utils.isInPortraitState() ? safeBounds.height + (self.workbenchView.frame.origin.y + self.workbenchView.frame.size.height) : safeBounds.width + (self.workbenchView.frame.size.width + self.workbenchView.frame.width))*/

let sDim = CGFloat(0)

var scDim = CGFloat(0)

if Utils.isIPad() {
if Utils.isInPortraitState() {
scDim = safeBounds.height - (self.workbenchView.frame.origin.y + 2 * (self.workbenchView.frame.size.height/3))
} else {
scDim = self.workbenchView.frame.origin.x + self.workbenchView.frame.size.width/3
}
} else {
if Utils.isInPortraitState() {
scDim = safeBounds.height - self.workbenchView.frame.size.height
} else {
scDim = safeBounds.width - self.workbenchView.frame.size.width
}
}
self.controlsView.setControlsType(controlsType: type, bounds: safeBounds, startingDim: sDim, scrolledDim: scDim)
}









share|improve this question
























  • Why not use constraints?
    – elbert rivas
    Nov 7 at 10:02










  • I tried to use them, but these functions are recalled from other parts of the code and do not accept them. I do not know how to solve
    – Stefano
    Nov 7 at 10:31










  • I don't really understand what do you want to accomplish. Do you want the UIScrollView to take the full screen of your iDevice or just have the same bounds as your blue rectangular view at the bottom of your screen?
    – YoanGJ
    Nov 7 at 12:28










  • @YoanGJ sorry. I want take my scrollview to the full screen with this code, but I can't understand frame propriety.. help please
    – Stefano
    Nov 7 at 12:38













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I need to set the layout of a ScrollView full-screen with autolayout. at the moment I have this code but not the full-height sect and I can not understand which variable to change. I have this code



func setControlsType(controlsType: ControlsType, bounds: CGRect, startingDim: CGFloat, scrolledDim: CGFloat) {
self.controlsType = controlsType
if Utils.isInPortraitState() {
/*self.startingFrame = CGRect(x: 0, y: bounds.height - startingDim, width: bounds.width, height: scrolledDim)
self.scrolledFrame = CGRect(x: 0, y: bounds.origin.y + bounds.height - scrolledDim, width: bounds.width, height: scrolledDim)*/
self.startingFrame = CGRect(x: 0, y: bounds.height, width: bounds.width, height: scrolledDim)
self.scrolledFrame = CGRect(x: 0, y: bounds.origin.y + bounds.height, width: bounds.width, height: scrolledDim)
} else {
self.startingFrame = CGRect(x: startingDim - scrolledDim, y: 0, width: scrolledDim, height: bounds.height)
self.scrolledFrame = CGRect(x: 0, y: 0, width: scrolledDim, height: bounds.height)
}


func setControls(type: ControlsType) {
let safeBounds = SafeAreaManager.letsDoIt(view: self, upon: .bounds)

/*let sDim = (Utils.isInPortraitState() ? safeBounds.height + (self.workbenchView.frame.origin.y + self.workbenchView.frame.size.height) : safeBounds.width + (self.workbenchView.frame.size.width + self.workbenchView.frame.width))*/

let sDim = CGFloat(0)

var scDim = CGFloat(0)

if Utils.isIPad() {
if Utils.isInPortraitState() {
scDim = safeBounds.height - (self.workbenchView.frame.origin.y + 2 * (self.workbenchView.frame.size.height/3))
} else {
scDim = self.workbenchView.frame.origin.x + self.workbenchView.frame.size.width/3
}
} else {
if Utils.isInPortraitState() {
scDim = safeBounds.height - self.workbenchView.frame.size.height
} else {
scDim = safeBounds.width - self.workbenchView.frame.size.width
}
}
self.controlsView.setControlsType(controlsType: type, bounds: safeBounds, startingDim: sDim, scrolledDim: scDim)
}









share|improve this question















I need to set the layout of a ScrollView full-screen with autolayout. at the moment I have this code but not the full-height sect and I can not understand which variable to change. I have this code



func setControlsType(controlsType: ControlsType, bounds: CGRect, startingDim: CGFloat, scrolledDim: CGFloat) {
self.controlsType = controlsType
if Utils.isInPortraitState() {
/*self.startingFrame = CGRect(x: 0, y: bounds.height - startingDim, width: bounds.width, height: scrolledDim)
self.scrolledFrame = CGRect(x: 0, y: bounds.origin.y + bounds.height - scrolledDim, width: bounds.width, height: scrolledDim)*/
self.startingFrame = CGRect(x: 0, y: bounds.height, width: bounds.width, height: scrolledDim)
self.scrolledFrame = CGRect(x: 0, y: bounds.origin.y + bounds.height, width: bounds.width, height: scrolledDim)
} else {
self.startingFrame = CGRect(x: startingDim - scrolledDim, y: 0, width: scrolledDim, height: bounds.height)
self.scrolledFrame = CGRect(x: 0, y: 0, width: scrolledDim, height: bounds.height)
}


func setControls(type: ControlsType) {
let safeBounds = SafeAreaManager.letsDoIt(view: self, upon: .bounds)

/*let sDim = (Utils.isInPortraitState() ? safeBounds.height + (self.workbenchView.frame.origin.y + self.workbenchView.frame.size.height) : safeBounds.width + (self.workbenchView.frame.size.width + self.workbenchView.frame.width))*/

let sDim = CGFloat(0)

var scDim = CGFloat(0)

if Utils.isIPad() {
if Utils.isInPortraitState() {
scDim = safeBounds.height - (self.workbenchView.frame.origin.y + 2 * (self.workbenchView.frame.size.height/3))
} else {
scDim = self.workbenchView.frame.origin.x + self.workbenchView.frame.size.width/3
}
} else {
if Utils.isInPortraitState() {
scDim = safeBounds.height - self.workbenchView.frame.size.height
} else {
scDim = safeBounds.width - self.workbenchView.frame.size.width
}
}
self.controlsView.setControlsType(controlsType: type, bounds: safeBounds, startingDim: sDim, scrolledDim: scDim)
}






swift xcode uiscrollview autolayout






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 11 at 13:49

























asked Nov 7 at 9:39









Stefano

228




228












  • Why not use constraints?
    – elbert rivas
    Nov 7 at 10:02










  • I tried to use them, but these functions are recalled from other parts of the code and do not accept them. I do not know how to solve
    – Stefano
    Nov 7 at 10:31










  • I don't really understand what do you want to accomplish. Do you want the UIScrollView to take the full screen of your iDevice or just have the same bounds as your blue rectangular view at the bottom of your screen?
    – YoanGJ
    Nov 7 at 12:28










  • @YoanGJ sorry. I want take my scrollview to the full screen with this code, but I can't understand frame propriety.. help please
    – Stefano
    Nov 7 at 12:38


















  • Why not use constraints?
    – elbert rivas
    Nov 7 at 10:02










  • I tried to use them, but these functions are recalled from other parts of the code and do not accept them. I do not know how to solve
    – Stefano
    Nov 7 at 10:31










  • I don't really understand what do you want to accomplish. Do you want the UIScrollView to take the full screen of your iDevice or just have the same bounds as your blue rectangular view at the bottom of your screen?
    – YoanGJ
    Nov 7 at 12:28










  • @YoanGJ sorry. I want take my scrollview to the full screen with this code, but I can't understand frame propriety.. help please
    – Stefano
    Nov 7 at 12:38
















Why not use constraints?
– elbert rivas
Nov 7 at 10:02




Why not use constraints?
– elbert rivas
Nov 7 at 10:02












I tried to use them, but these functions are recalled from other parts of the code and do not accept them. I do not know how to solve
– Stefano
Nov 7 at 10:31




I tried to use them, but these functions are recalled from other parts of the code and do not accept them. I do not know how to solve
– Stefano
Nov 7 at 10:31












I don't really understand what do you want to accomplish. Do you want the UIScrollView to take the full screen of your iDevice or just have the same bounds as your blue rectangular view at the bottom of your screen?
– YoanGJ
Nov 7 at 12:28




I don't really understand what do you want to accomplish. Do you want the UIScrollView to take the full screen of your iDevice or just have the same bounds as your blue rectangular view at the bottom of your screen?
– YoanGJ
Nov 7 at 12:28












@YoanGJ sorry. I want take my scrollview to the full screen with this code, but I can't understand frame propriety.. help please
– Stefano
Nov 7 at 12:38




@YoanGJ sorry. I want take my scrollview to the full screen with this code, but I can't understand frame propriety.. help please
– Stefano
Nov 7 at 12:38












1 Answer
1






active

oldest

votes

















up vote
1
down vote



accepted










It seems that this code is not in your view controller so I see two options among others:





  1. You pass your view and your scroll view to one of your function in the file that you shared above. Then you apply contraints to your scroll view so its frame match your view frame. This function will look like this:



    func resizeScrollView(view: UIView, scrollView: UIScrollView) {
    NSLayoutConstraint(item: scrollView, attribute: .top, relatedBy: .equal, toItem: view, attribute: .top, multiplier: 1, constant: 0).isActive = true
    NSLayoutConstraint(item: scrollView, attribute: .trailing, relatedBy: .equal, toItem: view, attribute: .trailing, multiplier: 1, constant: 0).isActive = true
    NSLayoutConstraint(item: scrollView, attribute: .bottom, relatedBy: .equal, toItem: view, attribute: .bottom, multiplier: 1, constant: 0).isActive = true
    NSLayoutConstraint(item: scrollView, attribute: .leading, relatedBy: .equal, toItem: view, attribute: .leading, multiplier: 1, constant: 0).isActive = true
    }



  2. Or you can access the bounds property of the screen of your device via UIScreen and in this case the function will look like this:



    func resizeScrollView(scrollView: UIScrollView) {
    scrollView.bounds = UIScreen.main.bounds
    }



Then you'll have to set the position of your scroll view in your view controller.






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',
    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%2f53186833%2fautolayout-scrollview-programmatically-swift%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








    up vote
    1
    down vote



    accepted










    It seems that this code is not in your view controller so I see two options among others:





    1. You pass your view and your scroll view to one of your function in the file that you shared above. Then you apply contraints to your scroll view so its frame match your view frame. This function will look like this:



      func resizeScrollView(view: UIView, scrollView: UIScrollView) {
      NSLayoutConstraint(item: scrollView, attribute: .top, relatedBy: .equal, toItem: view, attribute: .top, multiplier: 1, constant: 0).isActive = true
      NSLayoutConstraint(item: scrollView, attribute: .trailing, relatedBy: .equal, toItem: view, attribute: .trailing, multiplier: 1, constant: 0).isActive = true
      NSLayoutConstraint(item: scrollView, attribute: .bottom, relatedBy: .equal, toItem: view, attribute: .bottom, multiplier: 1, constant: 0).isActive = true
      NSLayoutConstraint(item: scrollView, attribute: .leading, relatedBy: .equal, toItem: view, attribute: .leading, multiplier: 1, constant: 0).isActive = true
      }



    2. Or you can access the bounds property of the screen of your device via UIScreen and in this case the function will look like this:



      func resizeScrollView(scrollView: UIScrollView) {
      scrollView.bounds = UIScreen.main.bounds
      }



    Then you'll have to set the position of your scroll view in your view controller.






    share|improve this answer



























      up vote
      1
      down vote



      accepted










      It seems that this code is not in your view controller so I see two options among others:





      1. You pass your view and your scroll view to one of your function in the file that you shared above. Then you apply contraints to your scroll view so its frame match your view frame. This function will look like this:



        func resizeScrollView(view: UIView, scrollView: UIScrollView) {
        NSLayoutConstraint(item: scrollView, attribute: .top, relatedBy: .equal, toItem: view, attribute: .top, multiplier: 1, constant: 0).isActive = true
        NSLayoutConstraint(item: scrollView, attribute: .trailing, relatedBy: .equal, toItem: view, attribute: .trailing, multiplier: 1, constant: 0).isActive = true
        NSLayoutConstraint(item: scrollView, attribute: .bottom, relatedBy: .equal, toItem: view, attribute: .bottom, multiplier: 1, constant: 0).isActive = true
        NSLayoutConstraint(item: scrollView, attribute: .leading, relatedBy: .equal, toItem: view, attribute: .leading, multiplier: 1, constant: 0).isActive = true
        }



      2. Or you can access the bounds property of the screen of your device via UIScreen and in this case the function will look like this:



        func resizeScrollView(scrollView: UIScrollView) {
        scrollView.bounds = UIScreen.main.bounds
        }



      Then you'll have to set the position of your scroll view in your view controller.






      share|improve this answer

























        up vote
        1
        down vote



        accepted







        up vote
        1
        down vote



        accepted






        It seems that this code is not in your view controller so I see two options among others:





        1. You pass your view and your scroll view to one of your function in the file that you shared above. Then you apply contraints to your scroll view so its frame match your view frame. This function will look like this:



          func resizeScrollView(view: UIView, scrollView: UIScrollView) {
          NSLayoutConstraint(item: scrollView, attribute: .top, relatedBy: .equal, toItem: view, attribute: .top, multiplier: 1, constant: 0).isActive = true
          NSLayoutConstraint(item: scrollView, attribute: .trailing, relatedBy: .equal, toItem: view, attribute: .trailing, multiplier: 1, constant: 0).isActive = true
          NSLayoutConstraint(item: scrollView, attribute: .bottom, relatedBy: .equal, toItem: view, attribute: .bottom, multiplier: 1, constant: 0).isActive = true
          NSLayoutConstraint(item: scrollView, attribute: .leading, relatedBy: .equal, toItem: view, attribute: .leading, multiplier: 1, constant: 0).isActive = true
          }



        2. Or you can access the bounds property of the screen of your device via UIScreen and in this case the function will look like this:



          func resizeScrollView(scrollView: UIScrollView) {
          scrollView.bounds = UIScreen.main.bounds
          }



        Then you'll have to set the position of your scroll view in your view controller.






        share|improve this answer














        It seems that this code is not in your view controller so I see two options among others:





        1. You pass your view and your scroll view to one of your function in the file that you shared above. Then you apply contraints to your scroll view so its frame match your view frame. This function will look like this:



          func resizeScrollView(view: UIView, scrollView: UIScrollView) {
          NSLayoutConstraint(item: scrollView, attribute: .top, relatedBy: .equal, toItem: view, attribute: .top, multiplier: 1, constant: 0).isActive = true
          NSLayoutConstraint(item: scrollView, attribute: .trailing, relatedBy: .equal, toItem: view, attribute: .trailing, multiplier: 1, constant: 0).isActive = true
          NSLayoutConstraint(item: scrollView, attribute: .bottom, relatedBy: .equal, toItem: view, attribute: .bottom, multiplier: 1, constant: 0).isActive = true
          NSLayoutConstraint(item: scrollView, attribute: .leading, relatedBy: .equal, toItem: view, attribute: .leading, multiplier: 1, constant: 0).isActive = true
          }



        2. Or you can access the bounds property of the screen of your device via UIScreen and in this case the function will look like this:



          func resizeScrollView(scrollView: UIScrollView) {
          scrollView.bounds = UIScreen.main.bounds
          }



        Then you'll have to set the position of your scroll view in your view controller.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 8 at 16:31

























        answered Nov 7 at 14:10









        YoanGJ

        189119




        189119






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53186833%2fautolayout-scrollview-programmatically-swift%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







            這個網誌中的熱門文章

            Tangent Lines Diagram Along Smooth Curve

            Yusuf al-Mu'taman ibn Hud

            Zucchini