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?
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);
}
Maybe you just typed this wrong, but your CGSize
has a height of 0?
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;
}
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.
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
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.