Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8855487
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T14:08:50+00:00 2026-06-14T14:08:50+00:00

I have a Scroll view in My Viewcontroller. Also I had created a view

  • 0

I have a Scroll view in My Viewcontroller. Also I had created a view and connected it to an IBOutlet variable. I need to add many views to the Scroll view.

Code for adding view to ScrollView :

In .h File

UIView *customView_;

@property (nonatomic, retain) IBOutlet UIView *customView;

In .m File :

@synthesize customView = customView_;

    for (int i = 0; i < 2; i++) {

        UIView *tempView = customView;
        tempView.frame = CGRectMake(i * 187, 0, 187, 133);
        [scrollView addSubview:tempView];
    }

But using the above code, I am only able to see one in the position : 187,0,187,133. If I change the code to for (int i = 0; i < 3; i++) { I am able to see the view only in 374,0,187,133. Can anyone please help me to Resolve this error?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T14:08:51+00:00Added an answer on June 14, 2026 at 2:08 pm

    Why is this happening?

    Documentation for addSubview:

    Views can have only one superview. If view already has a superview and that view is not the receiver, this method removes the previous superview before making the receiver its new superview.

    With this code

    UIView *tempView = customView;
    tempView.frame = CGRectMake(i * 187, 0, 187, 133);
    [scrollView addSubview:tempView];
    

    You are trying to add the same view twice/three times. With the comment from the documentation, this will remove the previous one, and add it to the new position. (Whilst the superview is the receiver in your case, it still does not mean it’ll copy your view and add it again).

    Solution

    Unfortunately, UIView does not follow the NSCopying protocol. As such, you can’t create a direct copy of it using copy. You will need to create a factory method that will create your view for you, as so:

    -(UIView)createCustomView {
        ...
    }
    

    Then call that in your for loop:

    UIView *tempView = [self createCustomView];
    tempView.frame = CGRectMake(i * 187, 0, 187, 133);
    [scrollView addSubview:tempView];
    

    This may outgrow your scope of the .xib file. Refer to this answer on how to get this to play nice with Interface Builder.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to add Scroll View into ViewController. I have created viewcontroller by code.
i have a scroll view with multiple images and i am using this code.
In my app I have a scroll view and four table views. Each time
I have a UIScrollView which has added viewController.view .How to get subview in scroll
I have a table view controller which doesn't let me manually scroll to the
i have a scroll view in my application and i want that when the
I have an scroll view with some sophisticated animations happening during scrolling. Although after
I have a scroll view with transparent background that covers most of the screen.
Suppose I have a scroll view in a view & a scroll view's size
I have a paged scroll view in my app and it works almost perfectly...

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.