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 6786581
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T17:15:46+00:00 2026-05-26T17:15:46+00:00

I am trying to create a UIScrollView programatically. I have a scrollView for that

  • 0

I am trying to create a UIScrollView programatically. I have a scrollView for that ViewController. Here is how I instantiate it in loadView:

    CGRect fullScreenRect = [[UIScreen mainScreen] applicationFrame];
    self.scrollView = [[UIScrollView alloc] initWithFrame:fullScreenRect];
    self.scrollView.contentSize = CGSizeMake(self.view.frame.size.width, self.view.frame.size.height);

I then some items onto it, and then add it to view in loadView as follows:

[self.scrollView addSubview:self.searchBar];
[self.scrollView addSubview:button];
[self.view addSubview:self.scrollView];

So far everything should be fine. However, in other functions, I will be adding ImageViews and then I will need to set the scrollview’s content size. The way I do it is just use [self.scrollView addToSubView:image] and when I want to edit the content size of the scrollview, I do it as follows:

// Calculate scroll view size
float sizeOfContent = 0;
for (int i = 0; i < [self.scrollView.subviews count]; i++) {
    UIImageView *view =[self.scrollView.subviews objectAtIndex:i];
    sizeOfContent += view.frame.size.height;
    [view release];
}

// Set content size for scroll view
self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width, sizeOfContent);

I keep getting an EXC_BAD_ACCESS error in the main.m class.

I suspect that the problem is with retaining the actual scrollView since I think scrollView is not retaining the same object that is a subview of view.

Any suggestions? Thanks in advance!

  • 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-05-26T17:15:47+00:00Added an answer on May 26, 2026 at 5:15 pm

    If you have defined self.scrollView as @property(nonatomic, retain) then first wrong thing is the line;

    self.scrollView = [[UIScrollView alloc] initWithFrame:fullScreenRect];
    

    It sets the retain count of scrollView to 2, which is not what you expect. alloc retained properties like this,

    scrollView = [[UIScrollView alloc] initWithFrame:fullScreenRect];
    

    I think your BAD_ACCESS error comes from here;

    for (int i = 0; i < [self.scrollView.subviews count]; i++) {
       UIImageView *view =[self.scrollView.subviews objectAtIndex:i];
       sizeOfContent += view.frame.size.height;
       [view release];
    }
    

    Why are you releasing view? You did not alloc it, so there is no reason to release it. Remove the [view release] line and check again.

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

Sidebar

Related Questions

I'm trying to create a view programmatically. The result that i want to have
I'm trying to create a paginated UIScrollView within a view that only scrolls horizontally
I am trying create a WCF service that leverages the WPF MediaPlayer on the
Ok so I am trying create a login script, here I am using PHP5
Trying to create my first iPhone app that would play back audio. When I
Trying to create a small monitor application that displays current internet usage as percentage
I am trying to create an app with horizontal scrolling, so that one would
I'm trying to create a view that's full of controls (buttons, text views, labels,
I'm trying create a box in my Django app that displays text (and possibly
I'm trying create a ASMX webservice that can perform a HTTP GET request. I

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.