iOS add imageView to scrollView












1















enter image description hereWhen i add three imageViews to scrollView, and i setting scrollView contentSize is three times scrollView's width, even if it was appeared in scrolView,but imageView's width not equal to scrollview's. I ensured that already setting they width equeal to each otehr. If who know that how to resolve this issue,please help me, thinks.



- (void)addScrollViewImage {
for (NSInteger index = 0; index < IMAGE_NUM; index++) {
UIImageView *imageView = [[UIImageView alloc] init];
CGFloat imageViewX = index * (self.scrollView.frame.size.width);
imageView.frame = CGRectMake(imageViewX, 0, self.scrollView.frame.size.width , self.scrollView.frame.size.height);
imageView.image = [UIImage imageNamed:@"搜索图片"];
imageView.contentMode = UIViewContentModeScaleAspectFill;
[self.scrollView addSubview:imageView];
}
self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width * IMAGE_NUM, self.scrollView.frame.size.height);


}










share|improve this question

























  • is autolayout enabled in your project ?

    – CodeChanger
    Nov 9 '16 at 8:55











  • Are you using constraints?

    – User511
    Nov 9 '16 at 9:07











  • Where "addScrollViewImage" method get called? In viewDidLoad()? Try to call in viewDidApprear(), where you will get the actual scrollview width.

    – miOS
    Nov 9 '16 at 9:28











  • yes ,i used Autolayout in my project. But only setting scrollview's constraints,imageview by addScrollViewImage: this function add.

    – nickpan
    Nov 9 '16 at 9:28
















1















enter image description hereWhen i add three imageViews to scrollView, and i setting scrollView contentSize is three times scrollView's width, even if it was appeared in scrolView,but imageView's width not equal to scrollview's. I ensured that already setting they width equeal to each otehr. If who know that how to resolve this issue,please help me, thinks.



- (void)addScrollViewImage {
for (NSInteger index = 0; index < IMAGE_NUM; index++) {
UIImageView *imageView = [[UIImageView alloc] init];
CGFloat imageViewX = index * (self.scrollView.frame.size.width);
imageView.frame = CGRectMake(imageViewX, 0, self.scrollView.frame.size.width , self.scrollView.frame.size.height);
imageView.image = [UIImage imageNamed:@"搜索图片"];
imageView.contentMode = UIViewContentModeScaleAspectFill;
[self.scrollView addSubview:imageView];
}
self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width * IMAGE_NUM, self.scrollView.frame.size.height);


}










share|improve this question

























  • is autolayout enabled in your project ?

    – CodeChanger
    Nov 9 '16 at 8:55











  • Are you using constraints?

    – User511
    Nov 9 '16 at 9:07











  • Where "addScrollViewImage" method get called? In viewDidLoad()? Try to call in viewDidApprear(), where you will get the actual scrollview width.

    – miOS
    Nov 9 '16 at 9:28











  • yes ,i used Autolayout in my project. But only setting scrollview's constraints,imageview by addScrollViewImage: this function add.

    – nickpan
    Nov 9 '16 at 9:28














1












1








1








enter image description hereWhen i add three imageViews to scrollView, and i setting scrollView contentSize is three times scrollView's width, even if it was appeared in scrolView,but imageView's width not equal to scrollview's. I ensured that already setting they width equeal to each otehr. If who know that how to resolve this issue,please help me, thinks.



- (void)addScrollViewImage {
for (NSInteger index = 0; index < IMAGE_NUM; index++) {
UIImageView *imageView = [[UIImageView alloc] init];
CGFloat imageViewX = index * (self.scrollView.frame.size.width);
imageView.frame = CGRectMake(imageViewX, 0, self.scrollView.frame.size.width , self.scrollView.frame.size.height);
imageView.image = [UIImage imageNamed:@"搜索图片"];
imageView.contentMode = UIViewContentModeScaleAspectFill;
[self.scrollView addSubview:imageView];
}
self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width * IMAGE_NUM, self.scrollView.frame.size.height);


}










share|improve this question
















enter image description hereWhen i add three imageViews to scrollView, and i setting scrollView contentSize is three times scrollView's width, even if it was appeared in scrolView,but imageView's width not equal to scrollview's. I ensured that already setting they width equeal to each otehr. If who know that how to resolve this issue,please help me, thinks.



- (void)addScrollViewImage {
for (NSInteger index = 0; index < IMAGE_NUM; index++) {
UIImageView *imageView = [[UIImageView alloc] init];
CGFloat imageViewX = index * (self.scrollView.frame.size.width);
imageView.frame = CGRectMake(imageViewX, 0, self.scrollView.frame.size.width , self.scrollView.frame.size.height);
imageView.image = [UIImage imageNamed:@"搜索图片"];
imageView.contentMode = UIViewContentModeScaleAspectFill;
[self.scrollView addSubview:imageView];
}
self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width * IMAGE_NUM, self.scrollView.frame.size.height);


}







ios uiscrollview






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 11 '16 at 9:18







nickpan

















asked Nov 9 '16 at 8:52









nickpannickpan

237




237













  • is autolayout enabled in your project ?

    – CodeChanger
    Nov 9 '16 at 8:55











  • Are you using constraints?

    – User511
    Nov 9 '16 at 9:07











  • Where "addScrollViewImage" method get called? In viewDidLoad()? Try to call in viewDidApprear(), where you will get the actual scrollview width.

    – miOS
    Nov 9 '16 at 9:28











  • yes ,i used Autolayout in my project. But only setting scrollview's constraints,imageview by addScrollViewImage: this function add.

    – nickpan
    Nov 9 '16 at 9:28



















  • is autolayout enabled in your project ?

    – CodeChanger
    Nov 9 '16 at 8:55











  • Are you using constraints?

    – User511
    Nov 9 '16 at 9:07











  • Where "addScrollViewImage" method get called? In viewDidLoad()? Try to call in viewDidApprear(), where you will get the actual scrollview width.

    – miOS
    Nov 9 '16 at 9:28











  • yes ,i used Autolayout in my project. But only setting scrollview's constraints,imageview by addScrollViewImage: this function add.

    – nickpan
    Nov 9 '16 at 9:28

















is autolayout enabled in your project ?

– CodeChanger
Nov 9 '16 at 8:55





is autolayout enabled in your project ?

– CodeChanger
Nov 9 '16 at 8:55













Are you using constraints?

– User511
Nov 9 '16 at 9:07





Are you using constraints?

– User511
Nov 9 '16 at 9:07













Where "addScrollViewImage" method get called? In viewDidLoad()? Try to call in viewDidApprear(), where you will get the actual scrollview width.

– miOS
Nov 9 '16 at 9:28





Where "addScrollViewImage" method get called? In viewDidLoad()? Try to call in viewDidApprear(), where you will get the actual scrollview width.

– miOS
Nov 9 '16 at 9:28













yes ,i used Autolayout in my project. But only setting scrollview's constraints,imageview by addScrollViewImage: this function add.

– nickpan
Nov 9 '16 at 9:28





yes ,i used Autolayout in my project. But only setting scrollview's constraints,imageview by addScrollViewImage: this function add.

– nickpan
Nov 9 '16 at 9:28












2 Answers
2






active

oldest

votes


















1














you need to change imageview content mode



imageView.contentMode =  UIViewContentModeScaleAspectFill;


With



imageView.contentMode =  UIViewContentModeScaleToFill;





share|improve this answer
























  • But i changed imageView's contentMode = UIViewContentModeScaleToFill, this problem exists too. I don't know that i have already setted it's correct orginX ,but there is a gap between imageView and scrollView.

    – nickpan
    Nov 11 '16 at 9:00



















0














This is because when your viewController is loaded, view / scrollView frame is still unknown.



You need to override viewDidLayoutSubviews() and setup children frames and content size there.



Important note: iOS is adding EXTRA UIImageView's for scroll indicators. That's why you can't use scrollView.subviews.count inside viewDidLayoutSubviews(), but you can use IMAGE_NUM though.



Some Swift code:



override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()

let scrollSize = scrollView.bounds.size
var frame = scrollView.bounds

// Wrong: for i in 0 ..< scrollView.subviews.count
for i in 0 ..< IMAGE_NUM {
let view = scrollView.subviews[i]
view.frame = frame
frame.origin.x += scrollSize.width
}

scrollView.contentSize = CGSize(width: frame.origin.x, height: scrollSize.height)
}





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%2f40502939%2fios-add-imageview-to-scrollview%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    you need to change imageview content mode



    imageView.contentMode =  UIViewContentModeScaleAspectFill;


    With



    imageView.contentMode =  UIViewContentModeScaleToFill;





    share|improve this answer
























    • But i changed imageView's contentMode = UIViewContentModeScaleToFill, this problem exists too. I don't know that i have already setted it's correct orginX ,but there is a gap between imageView and scrollView.

      – nickpan
      Nov 11 '16 at 9:00
















    1














    you need to change imageview content mode



    imageView.contentMode =  UIViewContentModeScaleAspectFill;


    With



    imageView.contentMode =  UIViewContentModeScaleToFill;





    share|improve this answer
























    • But i changed imageView's contentMode = UIViewContentModeScaleToFill, this problem exists too. I don't know that i have already setted it's correct orginX ,but there is a gap between imageView and scrollView.

      – nickpan
      Nov 11 '16 at 9:00














    1












    1








    1







    you need to change imageview content mode



    imageView.contentMode =  UIViewContentModeScaleAspectFill;


    With



    imageView.contentMode =  UIViewContentModeScaleToFill;





    share|improve this answer













    you need to change imageview content mode



    imageView.contentMode =  UIViewContentModeScaleAspectFill;


    With



    imageView.contentMode =  UIViewContentModeScaleToFill;






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 9 '16 at 10:51









    KKRocksKKRocks

    6,9861969




    6,9861969













    • But i changed imageView's contentMode = UIViewContentModeScaleToFill, this problem exists too. I don't know that i have already setted it's correct orginX ,but there is a gap between imageView and scrollView.

      – nickpan
      Nov 11 '16 at 9:00



















    • But i changed imageView's contentMode = UIViewContentModeScaleToFill, this problem exists too. I don't know that i have already setted it's correct orginX ,but there is a gap between imageView and scrollView.

      – nickpan
      Nov 11 '16 at 9:00

















    But i changed imageView's contentMode = UIViewContentModeScaleToFill, this problem exists too. I don't know that i have already setted it's correct orginX ,but there is a gap between imageView and scrollView.

    – nickpan
    Nov 11 '16 at 9:00





    But i changed imageView's contentMode = UIViewContentModeScaleToFill, this problem exists too. I don't know that i have already setted it's correct orginX ,but there is a gap between imageView and scrollView.

    – nickpan
    Nov 11 '16 at 9:00













    0














    This is because when your viewController is loaded, view / scrollView frame is still unknown.



    You need to override viewDidLayoutSubviews() and setup children frames and content size there.



    Important note: iOS is adding EXTRA UIImageView's for scroll indicators. That's why you can't use scrollView.subviews.count inside viewDidLayoutSubviews(), but you can use IMAGE_NUM though.



    Some Swift code:



    override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()

    let scrollSize = scrollView.bounds.size
    var frame = scrollView.bounds

    // Wrong: for i in 0 ..< scrollView.subviews.count
    for i in 0 ..< IMAGE_NUM {
    let view = scrollView.subviews[i]
    view.frame = frame
    frame.origin.x += scrollSize.width
    }

    scrollView.contentSize = CGSize(width: frame.origin.x, height: scrollSize.height)
    }





    share|improve this answer






























      0














      This is because when your viewController is loaded, view / scrollView frame is still unknown.



      You need to override viewDidLayoutSubviews() and setup children frames and content size there.



      Important note: iOS is adding EXTRA UIImageView's for scroll indicators. That's why you can't use scrollView.subviews.count inside viewDidLayoutSubviews(), but you can use IMAGE_NUM though.



      Some Swift code:



      override func viewDidLayoutSubviews() {
      super.viewDidLayoutSubviews()

      let scrollSize = scrollView.bounds.size
      var frame = scrollView.bounds

      // Wrong: for i in 0 ..< scrollView.subviews.count
      for i in 0 ..< IMAGE_NUM {
      let view = scrollView.subviews[i]
      view.frame = frame
      frame.origin.x += scrollSize.width
      }

      scrollView.contentSize = CGSize(width: frame.origin.x, height: scrollSize.height)
      }





      share|improve this answer




























        0












        0








        0







        This is because when your viewController is loaded, view / scrollView frame is still unknown.



        You need to override viewDidLayoutSubviews() and setup children frames and content size there.



        Important note: iOS is adding EXTRA UIImageView's for scroll indicators. That's why you can't use scrollView.subviews.count inside viewDidLayoutSubviews(), but you can use IMAGE_NUM though.



        Some Swift code:



        override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()

        let scrollSize = scrollView.bounds.size
        var frame = scrollView.bounds

        // Wrong: for i in 0 ..< scrollView.subviews.count
        for i in 0 ..< IMAGE_NUM {
        let view = scrollView.subviews[i]
        view.frame = frame
        frame.origin.x += scrollSize.width
        }

        scrollView.contentSize = CGSize(width: frame.origin.x, height: scrollSize.height)
        }





        share|improve this answer















        This is because when your viewController is loaded, view / scrollView frame is still unknown.



        You need to override viewDidLayoutSubviews() and setup children frames and content size there.



        Important note: iOS is adding EXTRA UIImageView's for scroll indicators. That's why you can't use scrollView.subviews.count inside viewDidLayoutSubviews(), but you can use IMAGE_NUM though.



        Some Swift code:



        override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()

        let scrollSize = scrollView.bounds.size
        var frame = scrollView.bounds

        // Wrong: for i in 0 ..< scrollView.subviews.count
        for i in 0 ..< IMAGE_NUM {
        let view = scrollView.subviews[i]
        view.frame = frame
        frame.origin.x += scrollSize.width
        }

        scrollView.contentSize = CGSize(width: frame.origin.x, height: scrollSize.height)
        }






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 14 '18 at 23:47

























        answered Nov 14 '18 at 23:36









        BorzhBorzh

        3,12813047




        3,12813047






























            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%2f40502939%2fios-add-imageview-to-scrollview%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

            Florida Star v. B. J. F.

            Danny Elfman

            Lugert, Oklahoma