11

Recently i've downloaded Xcode 4.5(iOS 6),but when tried to continue one of my projects i noticed that the UIScrollView isn't working properly,let me explain more clearly:

I've created the UIScrollView and added the initialized it using the following code:

-(void)viewDidLoad{

    [mainScrollView setScrollEnabled:YES];
    mainScrollView.contentSize = CGSizeMake(480, 0);

    [super viewDidLoad];

}

After i opened my XIB and linked the mainScrollView to a UIScrollView,after i added some UIButton's to the scrollView.When i built the project the mainScrollView was scrolling but the buttons didn't showed up,i mean,i just could see the button that were already on the screen.What i'm doing wrong?How can i setup the UIScrollView?

Mateus
  • 2,640
  • 5
  • 44
  • 62

6 Answers6

39

For xcode 4.5 ,If you are using scrollView you have to either uncheck "use AutoLayout" in file inspector.

or if you want to "use AutoLayout",then in .m(implementation) file add this

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    theScroller.contentSize=CGSizeMake(1024.0,1300.0);
}
Dinesh Raja
  • 8,501
  • 5
  • 42
  • 81
user1489709
  • 407
  • 4
  • 10
  • 1
    Thank you, thank you, thank you!! Banging my head against a brick wall for a hour or more with this. Again, thank you. – PaulJ Oct 05 '12 at 09:52
  • 2
    OK, so the answer for AutoLayout is found here: http://stackoverflow.com/questions/12619786/embed-imageview-in-scrollview-with-auto-layout-on-ios-6 basically, move the `contentSize` code to the `viewDidAppear` method. – PaulJ Oct 05 '12 at 10:12
  • THANK YOU for this post!!!! I have literally burned an entire day with this. I unchecked "use AutoLayout" and finally my scrollView works like its supposed to. Burns me up... TY again – tg2007 Oct 26 '12 at 04:15
4

Maybe you just typed this wrong, but your CGSize has a height of 0?

Alexsander Akers
  • 15,967
  • 12
  • 58
  • 83
4

You can do it with Auto Layout in XCode 4.5 But you config your UIScrollView in viewDidAppear

I do this also in a static UITableView with images.

Name your images like timg1.png, timg2.png....

in your ControllerView.h File

@property (weak, nonatomic) IBOutlet UIScrollView *Sv1;
@property (weak, nonatomic) IBOutlet UIPageControl *Pc1;

In your ControllerView.m File

-(void)viewDidAppear:(BOOL)animated
{

    //Scrollview
    Sv1.delegate = self;

    Sv1.contentSize = CGSizeMake(260, 176);

    [self.Sv1 setBackgroundColor:[UIColor clearColor]];
    // ScrollViewGarten.frame = CGRectMake(9, 11, 283, 170);
    [Sv1 setCanCancelContentTouches:NO];

    Sv1.multipleTouchEnabled = NO;
    Sv1.indicatorStyle = UIScrollViewIndicatorStyleWhite;
    Sv1.clipsToBounds = YES;
    Sv1.scrollEnabled = YES;
    Sv1.pagingEnabled = YES;

    NSUInteger nimagesTextil = 0;
    CGFloat cxTextil = 0;
    for (; ; nimagesTextil++) {
        NSString *imageNameTextil = [NSString stringWithFormat:@"timg%d.png", (nimagesTextil + 1)];
        UIImage *imageTextil = [UIImage imageNamed:imageNameTextil];
        if (imageTextil == nil) {
            break;
        }
        UIImageView *imageViewTextil = [[UIImageView alloc] initWithImage:imageTextil];

        CGRect rect = imageViewTextil.frame;
        rect.size.height = 176;
        rect.size.width = 260;
        rect.origin.x = ((Sv1.frame.size.width - imageTextil.size.width) / 2) + cxTextil;
        rect.origin.y = ((Sv1.frame.size.height - imageTextil.size.height) / 2);

        imageViewTextil.frame = rect;

        [Sv1 addSubview:imageViewTextil];

        cxTextil += Sv1.frame.size.width;
    }
    self.Pc1.numberOfPages = nimagesTextil;
    [Sv1 setContentSize:CGSizeMake(cxTextil, [Sv1 bounds].size.height)];
    self.Pc1.currentPage = 0;

}
Frank M.
  • 71
  • 3
3

I believe this is a bug with either Xcode 4.5 or the iOS 6 SDK. The way I fixed it was by adding the subviews to my UIScrollView programatically.

So you would have to do something like:

- (void)viewDidLoad {

    [super viewDidLoad];

    UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(400.0f, 10.0f, 60.0f, 30.0f);
    [mainScrollView addSubView:button];
    mainScrollView.contentSize = CGSizeMake(480.0f, mainScrollView.frame.size.height);
}

Try this and let me know if this worked for you.

If you have an old version of Xcode it might be a good idea to replicate the code and the nib over there to see the results.

Pedro Mancheno
  • 5,237
  • 4
  • 24
  • 31
3

If you would like to fix this AND use AutoLayout, move your contentSize code to the viewDidAppear method. I found the answer in this SO question: Embed ImageView in ScrollView with Auto Layout on iOS 6

Community
  • 1
  • 1
PaulJ
  • 2,869
  • 1
  • 24
  • 21
0

I would do it like this:

-(void)viewDidLayoutSubviews
{
[self.scrollview setScrollEnabled:YES];
[self.scrollview setUserInteractionEnabled:YES];
[self.scrollview setContentSize:CGSizeMake(320, _y)];
[self.scrollview setAlwaysBounceVertical:YES];
}

I use the class variable _y since I add all the subviews heights to it.

Pedroinpeace
  • 1,419
  • 1
  • 14
  • 20