UIScrollView behave strange - ios

I set UIScrollView's subview's height to 768,and UIScrollView's contentSize is 1024*768 .But when I swipe up or swipe down,the subview(UIImageView) will scroll up or down a little.I don't know why.
here is my code
-(void)viewDidLoad
{
[super viewDidLoad];
// scrollView is an outlet of UIScrollView
[scrollView setBackgroundColor:[UIColor blackColor]];
[scrollView setCanCancelContentTouches:NO];
scrollView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
scrollView.clipsToBounds = YES; // default is NO, we want to restrict drawing within our scrollview
scrollView.scrollEnabled = YES;
// pagingEnabled property default is NO, if set the scroller will stop or snap at each photo
// if you want free-flowing scroll, don't set this property.
scrollView.pagingEnabled = YES;
// load all the images from our bundle and add them to the scroll view
NSUInteger i;
imageCount=3;
for (i = 1; i <= imageCount; i++)
{
NSString *imageName = [NSString stringWithFormat:#"tu%d_s.png", i];
UIImage *image = [UIImage imageNamed:imageName];
if(!image)
{
NSLog(#"%# is nil",imageName);
}
UIImageView *TempImageView = [[UIImageView alloc] initWithImage:image];
// setup each frame to a default height and width, it will be properly placed when we call "updateScrollList"
CGRect rect = TempImageView.frame;
rect.size.height = 768;
rect.size.width = 1024;
TempImageView.frame = rect;
TempImageView.tag = i; // tag our images for later use when we place them in serial fashion
[scrollView addSubview:TempImageView];
NSLog(#"viewDidLoad TempImageView width %f,height %f,x %f,y %f",TempImageView.frame.size.width,TempImageView.frame.size.height,TempImageView.frame.origin.x,TempImageView.frame.origin.y);
}
[self layoutScrollImages]; // now place the photos in serial layout within the scrollview
}
- (void)layoutScrollImages
{
UIImageView *view = nil;
NSArray *subviews = [scrollView subviews];
// reposition all image subviews in a horizontal serial fashion
CGFloat curXLoc = 0;
for (view in subviews)
{
if ([view isKindOfClass:[UIImageView class]] && view.tag > 0)
{
CGRect frame = view.frame;
frame.origin = CGPointMake(curXLoc, 0);
view.frame = frame;
curXLoc += (1024);
}
NSLog(#"shang2 layoutScrollImages width %f,height %f,x %f,y %f",view.frame.size.width,view.frame.size.height,view.frame.origin.x,view.frame.origin.y);
}
// set the content size so it can be scrollable
[scrollView setContentSize:CGSizeMake((imageCount * 1024), 768)];
NSLog(#"shang2 content width %f,height %f,x %f,y %f",scrollView.contentSize.width,scrollView.contentSize.height,scrollView.frame.origin.x,scrollView.frame.origin.y);
}
here is Xcode log
2013-04-03 20:50:23.939 animation[1464:c07] viewDidLoad TempImageView width 1024.000000,height 768.000000,x 0.000000,y 0.000000
2013-04-03 20:50:23.952 animation[1464:c07] viewDidLoad TempImageView width 1024.000000,height 768.000000,x 0.000000,y 0.000000
2013-04-03 20:50:23.995 animation[1464:c07] viewDidLoad TempImageView width 1024.000000,height 768.000000,x 0.000000,y 0.000000
2013-04-03 20:50:23.995 animation[1464:c07] shang2 layoutScrollImages width 7.000000,height 5.000000,x 741.000000,y 1019.000000
2013-04-03 20:50:23.996 animation[1464:c07] shang2 layoutScrollImages width 1024.000000,height 768.000000,x 0.000000,y 0.000000
2013-04-03 20:50:23.996 animation[1464:c07] shang2 layoutScrollImages width 1024.000000,height 768.000000,x 1024.000000,y 0.000000
2013-04-03 20:50:23.996 animation[1464:c07] shang2 layoutScrollImages width 1024.000000,height 768.000000,x 2048.000000,y 0.000000
2013-04-03 20:50:23.996 animation[1464:c07] shang2 content width 3072.000000,height 768.000000,x 0.000000,y 0.000000

If your view controller containing the scrollview has either a UINavigationController or UITabBarController, the view will resize itself to fit the bounds which after the 44 pixels from the navigation controller would be 724, therefore the scrollview can scroll 44 pixels.

Related

All my UIView frames have height and width of 0

I have a storyboard with a view, but all my UIViews (actually UIViewTable and UIScrollView) appear to have a frame.width and frame.height of 0.
I can clearly see them in the simulator.
- (void) viewDidAppear:(BOOL)animated{
// check frame size of tableView
NSLog(#"Frame width = %f, height = %f", self.tableView.frame.size.width, self.tableView.frame.size.height);
// check contentSize of tableView
NSLog(#"contentSize width = %f, height = %f", self.tableView.contentSize.width, self.tableView.contentSize.height);
// check frame size of scrollView
NSLog(#"scrollView size - width = %f, height = %f", self.scrollView.frame.size.width, self.scrollView.frame.size.height);
[super viewDidAppear:animated];
}
All these logs show 0 for the height and the width.

UIScrollview doesn't scroll iOS

I'm creating several scroll views in one view that scroll horizontally. The following code works fine:
-(void)updateSection {
builder = [NSMutableArray array];
float xPosition = 0;
float xposbut = 100;
scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 100, self.frame.size.width, self.frame.size.height - 69)];
scrollView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
scrollView.delegate = self;
[self addSubview:scrollView];
for (int i = 1; i < itemArticleArray.count; i++) {
ItemView *item = [ItemView loadNibs];
item.frame = CGRectMake(xposbut, 10, item.frame.size.width, item.frame.size.height);
xPosition += scrollView.frame.size.width + 2;
xposbut += 500;
UIView *seperatorView = [[UIView alloc] initWithFrame:CGRectMake(xposbut - 50, 4, 2, scrollView.frame.size.height - 8)];
[scrollView addSubview:seperatorView];
xPosition += scrollView.frame.size.width + 4;
[scrollView addSubview: item];
[builder addObject:item];
}
[scrollView setContentSize:CGSizeMake(xposbut, scrollView.frame.size.height)];
[self addSubview:scrollView];
}
However, when I change the 8th line of code to the following:
scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 400, self.frame.size.width, self.frame.size.height - 69)];
It cause the layout to look fine, but the scroll views do then not scroll at all. Any ideas?
you set the contentSize.height to the scrollView.frame.size.height and so the scrollView cant scroll. you have to set the contentSize to the total height of you scrollView, including the not visible area. The frame is only the area on screen.
UIScrollview will scroll when content size is bigger then frame size. In your code you set content size equal to frame size that's why your scrollview is not scrolling.
Set the content size for the scroll view. The content size should be greater than frame.size, then only the scroll view will scroll. And also enable scrolling.
Use the code
scrollView.scrollEnabled=YES;
scrollView.contentSize=CGSizeMake(CGFloat width, CGFloat height);
Height should be greater than scrollView.frame.size.height and add contents inside scrollview beyond the scrolview's frame.
You must give the scroll some space to work so check this out:
Create the scroll in the storyboard
Create an outlet for the scroll
And the in the .m file you must paste these lines
The code:
(void)viewDidLoad {
[super viewDidLoad];
yourScroll.scrollEnabled=YES;
yourScroll.contentSize=CGSizeMake(self.view.frame.size.width, self.view.frame.size.height*2);
}
/*
* yourScroll.contentSize=CGSizeMake(your desired scroll width, your desired scroll height);
* The values must be bigger than the viewController size
*/

Resize UIScrollView with setcontentsize

I am trying to resize UIScrollView inside a UICOllectionViewCell but it doesn't resize it.
-(int)resizeScrollView:(int)height{
NSLog(#"Before Scroll height %f",self.scrollView.frame.size.height);
float currentHeight=self.scrollView.frame.size.height+height+200;
NSLog(#"height %f",currentHeight);
CGSize size=CGSizeMake(self.scrollView.frame.size.width ,
currentHeight + 10 );
[self.scrollView setContentSize:size];
self.scrollView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
NSLog(#"After Scroll height %f",self.scrollView.frame.size.height);
return currentHeight;
}
Setting the contentSize adjusts the amount of logical space that the scroll view scrolls over. You need to set the frame or the bounds to adjust the size of the scrollview in its parent view.
CGRect scrollFrame = self.scrollView.frame;
scrollFrame.size.height += height + 210;
self.scrollView.frame = scrollFrame;

How to add multiple UIImageViews to paging UIScrollView?

I have a UIScrollView with paging enabled with multiple subviews on the page: a UIButton, UIwebview and UIImageView. Both the webview and the image change on every page. This works fine. I used Apples scrolling image paging example to get me started.
But when I add a second UIImageView, the position of image I have in place already gets the new values and the new image doesn't display.
This is the code inside viewdidload for the first image (works fine):
// load all the images from our bundle and add them to the scroll view
for (i = 1; i <= kNumImages; i++)
{
NSString *imageName = [NSString stringWithFormat:#"image%d.jpg", i];
UIImage *image2 = [UIImage imageNamed:imageName];
UIImageView *imageView2 = [[UIImageView alloc] initWithImage:image2];
// setup each frame to a default height and width, it will be properly placed when we call "updateScrollList"
CGRect rect = imageView2.frame;
rect.size.height = imageviewScrollObjHeight;
rect.size.width = imageviewScrollObjWidth;
// Get the Layer of any view
CALayer * imageLayer = [imageView2 layer];
[imageLayer setMasksToBounds:YES];
[imageLayer setCornerRadius:7.0];
// You can even add a border
[imageLayer setBorderWidth:1.0];
[imageLayer setBorderColor:[[UIColor lightGrayColor] CGColor]];
imageView2.frame = rect;
imageView2.tag = i; // tag our images for later use when we place them in serial fashion
[scrollView1 addSubview:imageView2];
}
[self layoutScrollImages]; // now place the photos in serial layout within the scrollview
This is the code to layout the first image on every page, different image per page(outside viewdidload)(works fine):
// layout images for imageview1
- (void)layoutScrollImages
{
UIImageView *imageView = nil;
NSArray *subviews = [scrollView1 subviews];
// reposition all image subviews in a horizontal serial fashion
CGFloat curXLoc = 10;
for (imageView in subviews)
{
if ([imageView isKindOfClass:[UIImageView class]] && imageView.tag > 0)
{
CGRect frame = imageView.frame;
frame.origin = CGPointMake(curXLoc, 50);
imageView.frame = frame;
curXLoc += (kScrollObjWidth);
}
}
// set the content size so it can be scrollable
[scrollView1 setContentSize:CGSizeMake((kNumImages * kScrollObjWidth), [scrollView1 bounds].size.height)];
}
This is the code for the second image (inside viewdidload): (when i remove [self layoutNavScrollImages]; the image loads only on the first page)
for (i = 1; i <= kNumImages; i++)
{
UIImage *navBarImage = [UIImage imageNamed:#"navigationbar.png"];
UIImageView *imageViewNavBar = [[UIImageView alloc] initWithImage:navBarImage];
// setup each frame to a default height and width, it will be properly placed when we call "updateScrollList"
CGRect navBarRect = imageViewNavBar.frame;
navBarRect.size.height = 44;
navBarRect.size.width = 320;
navBarRect.origin.x = 0;
navBarRect.origin.y = 0;
/* Get the Layer of any view
CALayer * imageLayer = [imageView3 layer];
[imageLayer setMasksToBounds:YES];
[imageLayer setCornerRadius:7.0];
// You can even add a border
[imageLayer setBorderWidth:1.0];
[imageLayer setBorderColor:[[UIColor lightGrayColor] CGColor]];
*/
imageViewNavBar.frame = navBarRect;
imageViewNavBar.tag = i; // tag our images for later use when we place them in serial fashion
[scrollView1 addSubview:imageViewNavBar];
}
[self layoutNavScrollImages];
And the code outside viewdidload:(this overwrites the position of the first image)
- (void)layoutNavScrollImages
{
UIImageView *view = nil;
NSArray *subviews = [scrollView1 subviews];
// reposition all image subviews in a horizontal serial fashion
CGFloat curXLoc = 0;
for (view in subviews)
{
if ([view isKindOfClass:[UIImageView class]] && view.tag > 0)
{
CGRect frame = view.frame;
frame.origin = CGPointMake(curXLoc, 0);
view.frame = frame;
curXLoc += (kScrollObjWidth);
}
}
// set the content size so it can be scrollable
[scrollView1 setContentSize:CGSizeMake((kNumImages * kScrollObjWidth), [scrollView1 bounds].size.height)];
}
The way to do this is to make one (big) UIView and add every UIImageView as a subview of that UIView. And then add the UIView as a subview to UIScrollView.
[totalView addSubview:imageView1];
[totalView addSubview:imageView2;
[totalView addSubview:buttonView1];
[totalView addSubview:buttonView2];
[totalView addSubview:webView];
[scrollView addSubview:totalView];
Be sure to set the right content size or scrollview wont work:
[scrollView setContentSize:CGSizeMake((320*kNumImages), 411)];
Setup views and tag the UIView (inside viewdidload):
NSUInteger i;
for (i = 1; i <= kNumImages; i++)
{
//... code to setup views
totalView.tag = i;
}
[self layoutViews]; // now place the views in serial layout within the scrollview
Then to layout the view on every page:
- (void)layoutViews
{
UIView *view = nil;
NSArray *subviews = [scrollView subviews];
// reposition all image subviews in a horizontal serial fashion
CGFloat curXLoc = 0;
for (view in subviews)
{
if ([view isKindOfClass:[UIView class]] && view.tag > 0)
{
CGRect frame = view.frame;
frame.origin = CGPointMake(curXLoc, 0);
view.frame = frame;
curXLoc += (kScrollObjWidth);
}
}
}
I hope this is as clear as possible. If anyone thinks this is not the right way to do it please comment.

IOS Add subview on viewDidLoad

i have a problem when i'm trying to add subviews to a UIScrollView on viewDidLoad.
I'm using this code to programmatically add the UIImageViews to the scrollView:
- (void)viewDidLoad {
[super viewDidLoad];
NSInteger const_width = 100;
NSInteger numberOfViews = 4;
CGRect theFrame = [self.scrollView frame];
for (int i = 0; i < numberOfViews; i++) {
CGFloat xOrigin = i * const_width;
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(xOrigin,theFrame.origin.y,const_width,110)];
UIImage *image = [UIImage imageNamed:#"cocoloco.jpg"];
[imageView setImage:image];
//[self.scrollView addSubview:imageView];
imageView.tag = i;
CGRect rect = imageView.frame;
rect.size.height = 110;
rect.size.width = 110;
imageView.frame = rect;
[self.scrollView addSubview:imageView];
}
self.scrollView.contentSize = CGSizeMake(const_width * numberOfViews, 110);}
But i get the current view:
It seems that the scroll view frame takes its position regardless the 3 yellow tabs (that are a special TabBarController) so i get a wrong frame origin from the UIScrollView and therefore the UIImageViews are wrong positioned.
Any idea?
I don't know exactly how to do it, but add the height of the frame to the "Y" position of your rectangle. Something like this:
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(xOrigin,theFrame.origin.y + (theFrame.height),const_width,110)];
To get the scrollview below the navigation bar at the top, try
self.scrollview.autoresizingMask = UIViewAutoresizingFlexibleTopMargin;
Then position the table below the scrollview by setting it's origin base on the scrollview origin and height:
CGRect frame = tableView.frame;
frame.origin.y = self.scrollview.frame.origin.y + self.scrollview.frame.size.height;
tableView.frame = frame;
You should move any resizing or layout of your UI to the -(void)layoutSubviews method and this should sort your problem correctly.

Resources