UIScrollView Not scrolling to the bottom - ios

I have a scrollview set up that holds 20 sunglasses, when calling the function, it brings up a new view with the sunglasses 2 by 2 with 20 in total (with 19 and 20 being at the bottom of the scrollview).
It works perfectly on iPhone & iPhone5 but on the iPad, it only goes to 16, if I try and drag further, I can see the remaining 4 sunglasses, but I cannot select them, as the scrollview just bounces back to the 'bottom' which it thinks is 16.
This is on an iPad Air, so I am not sure if it an iPad 2 would have this issue with the different resolution.
This is how I am calling the view;
- (IBAction)glassesClick:(id)sender
{
[(AppDelegate*)[[UIApplication sharedApplication] delegate] playSoundEffect:1];
[scroll scrollRectToVisible:CGRectMake(0, 0, 5, 5) animated:NO];
glassesView.frame = CGRectMake(0, -[[UIScreen mainScreen] bounds].size.height, [[UIScreen mainScreen] bounds].size.width, [[UIScreen mainScreen] bounds].size.height);
[self.view addSubview:glassesView];
[UIView animateWithDuration:0.5 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^
{
glassesView.frame = CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width, [[UIScreen mainScreen] bounds].size.height);
} completion:nil];
}
Scroll container view setting;
{
for(UIView *view in [scroll subviews])
[view removeFromSuperview];
scroll.contentSize = CGSizeMake(scroll.frame.size.width, 1760);
for(int i = 0; i < 20; i++)
{
int xCoord = 0;
if(i % 2 == 0)
xCoord = 57;
else
xCoord = 188;
UIButton *glasss = [[UIButton alloc] initWithFrame:CGRectMake(xCoord, 20 + i/2 * 150 + i/2 * 20, 75, 150)];
if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
int xCoord = 0;
if(i % 2 == 0)
xCoord = 156;
else
xCoord = 462;
glasss.frame = CGRectMake(xCoord, 20 + i/2 * 300 + i/2 * 20, 150, 300);
scroll.contentSize = CGSizeMake(scroll.frame.size.width, 2580);
}
glasss.tag = i + 1;
[glasss addTarget:self action:#selector(glassChosen:) forControlEvents:UIControlEventTouchUpInside];
[glasss setImage:[UIImage imageNamed:[NSString stringWithFormat:#"glass%d.png", i + 1]] forState:UIControlStateNormal];
[[glasss imageView] setContentMode:UIViewContentModeScaleAspectFit];
if(i >= 10)
{
if([[[NSUserDefaults standardUserDefaults] objectForKey:#"purchased_glasses"] isEqualToString:#"NO"])
{
UIImageView *locked = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 20, 25)];
locked.image = [UIImage imageNamed:#"lock_button.png"];
[glasss addSubview:locked];
[locked release];
[glasss removeTarget:self action:#selector(glassChosen:) forControlEvents:UIControlEventTouchUpInside];
[glasss addTarget:self action:#selector(chooseLockedFlavor) forControlEvents:UIControlEventTouchUpInside];
}
}
UIImageView *backGlass = [[UIImageView alloc] initWithFrame:glasss.frame];
backGlass.image = [UIImage imageNamed:#"back_glass.png"];
[backGlass setContentMode:UIViewContentModeScaleAspectFit];
[scroll addSubview:backGlass];
[backGlass release];
[scroll addSubview:glasss];
[glasss release];
}
}

In this segment:
scroll.contentSize = CGSizeMake(scroll.frame.size.width, 1760);
We discovered you're explicitly setting the height of the contentSize. As such, you're probably blocking your iPad view. Try setting it dynamically (or not setting it at all!).

Set scrollView contentSize in viewDidLayoutSubviews instead of viewDidLoad.
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
scroll.contentSize = CGSize(scroll.frame.size.width, 1760)
}

Related

UIView coordinate conversion has an exception. convertPoint: toView: not work well

I try to make a navigation bar indicator view animation. The layout has some problem.
Expects this:
My solution is to convert the position in the navigation bar's bottom position to self.navigationItem.titleView.
The navigation bar indicator view should belong to the view controller. Not the navigation controller. So indicator view is on self.navigationItem.titleView.
While its position is the navigation bar's bottom. Y position is Point (0, 64) exactly.
So a method convertPoint: toView: is what I need .
Here is code:
- (void)setupTitlesView
{
UIView *titlesView = [[UIView alloc] initWithFrame: CGRectMake(0, 0, 100, 35)];
self.navigationItem.titleView = titlesView;
CGPoint new_point = [self.view convertPoint: CGPointMake(0, 64) toView: self.navigationItem.titleView];
NSLog(#"\n\n%#\n\n", NSStringFromCGPoint(new_point));
UIView *indicatorView = [[UIView alloc] initWithFrame: CGRectMake(0, new_point.y - 2, 0, 2)];
indicatorView.tag = -1;
self.indicatorView = indicatorView;
NSArray *titles = #[#"商品", #"详情"];
CGFloat width = titlesView.frame.size.width / titles.count;
CGFloat height = titlesView.frame.size.height;
for (NSInteger i = 0; i<titles.count; i++) {
UIButton *button = [[UIButton alloc] initWithFrame: CGRectMake(i*width, 0, width, height)];
button.tag = i;
[button setTitle:titles[i] forState:UIControlStateNormal];
// color , font ...
[titlesView addSubview:button];
// default click
if (i == 0) {
button.enabled = NO;
self.selectedButton = button;
[button.titleLabel sizeToFit];
CGRect tempFrame = self.indicatorView.frame;
tempFrame.size.width = button.titleLabel.frame.size.width;
self.indicatorView.frame = tempFrame;
CGPoint tempCenter = self.indicatorView.center;
tempCenter.x = button.center.x;
self.indicatorView.center = tempCenter;
}
}
[titlesView addSubview: indicatorView];
}
The result prints :
{0, 64}
Apple says:
convertPoint:toView: Converts a point from the receiver’s coordinate
system to that of the specified view.
How to fix it?
If I were you,I would try:
- (void)setupTitlesView
{
UIView *titlesView = [[UIView alloc] initWithFrame: CGRectMake(0, 0, 100, 35)];
self.navigationItem.titleView = titlesView;
UIView *indicatorView = [[UIView alloc] initWithFrame: CGRectMake(0, titlesView.frame.size.height - 2, 50, 2)];
indicatorView.tag = -1;
self.indicatorView = indicatorView;
self.indicatorView.backgroundColor = [UIColor blueColor];
NSArray *titles = #[#"商品", #"详情"];
CGFloat width = titlesView.frame.size.width / titles.count;
CGFloat height = titlesView.frame.size.height;
for (NSInteger i = 0; i<titles.count; i++) {
UIButton *button = [[UIButton alloc] initWithFrame: CGRectMake(i*width, 0, width, height)];
button.tag = i+1000;
[button setTitle:titles[i] forState:UIControlStateNormal];
[button setTitleColor:[UIColor blueColor] forState:UIControlStateSelected];
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[button addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchDown];
[titlesView addSubview:button];
// default click
if (i == 0) {
button.selected = YES;
}
}
[titlesView addSubview: indicatorView];
}
- (void)buttonClicked:(UIButton *)sender
{
for (int i = 0; i < 2; i++) {
UIButton *button = [self.navigationItem.titleView viewWithTag:(1000+i)];
button.selected = (button.tag == sender.tag);
}
[UIView animateWithDuration:0.3 animations:^{
self.indicatorView.center = CGPointMake(sender.center.x, self.indicatorView.center.y);
}];
}

Fix next image in scroll view like in page view

I created a scroll view , but i want to made it to act like a page view .
- (void)setupScrollView:(UIScrollView*)scrMain {
for (int i=1; i<=11; i++) {
image = [UIImage imageNamed:[NSString stringWithFormat:#"%d.png",i]];
if (i==1) {
imgV = [[UIImageView alloc] initWithFrame:CGRectMake(20, 2, 289, 470)];
}
else
imgV = [[UIImageView alloc] initWithFrame:CGRectMake((i-1)*350, 2, 289, 470)];
imgV.contentMode=UIViewContentModeScaleToFill;
[imgV setImage:image];
imgV.tag=i+1;
[imgV bringSubviewToFront:self.aleatoire];
[scrMain addSubview:imgV];
UIImageView *aleatoire=[[UIImageView alloc]initWithFrame:CGRectMake (self.aleatoire.frame.origin.x-19,self.aleatoire.frame.origin.y,self.aleatoire.frame.size.width,self.aleatoire.frame.size.height)];
aleatoire.image=self.aleatoire.image;
[imgV addSubview:aleatoire];
[imgV bringSubviewToFront:aleatoire];
UIButton *rand = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[rand addTarget:self
action:#selector(randomBtn:)
forControlEvents:UIControlEventTouchUpInside];
rand.frame=aleatoire.frame;
rand.backgroundColor=[UIColor clearColor];
rand.userInteractionEnabled = YES;
imgV.userInteractionEnabled=YES;
[imgV addSubview:rand];
[imgV bringSubviewToFront:rand];
}
[scrMain setContentSize:CGSizeMake(scrMain.frame.size.width*11, scrMain.frame.size.height)];
}
but i have this issue:
the next image is not in the center . Any ideea where i'am wrong ?
You set UIScrollView paging pagingEnabled property enable to create scroll behavior like pageView.
try this code :
scrMain.pagingEnabled=YES;
scrMain.contentOffset = CGPointMake([[UIScreen mainScreen] bounds].size.width, 0); // set your size to scroll content
Simple way to create pagerView in UIScrollView
int x = 0;
for (int i=1; i<=11; i++) {
UIImageView *img = [[UIImageView alloc]initWithFrame:CGRectMake(x, 0, [[UIScreen mainScreen] bounds].size.width,[[UIScreen mainScreen] bounds].size.height)];
img.image = [UIImage imageNamed:[NSString stringWithFormat:#"%d.png",i]];
[self.scrollView addSubview:img];
x = x + [[UIScreen mainScreen] bounds].size.width;
}
self.scrollView.contentSize = CGSizeMake(x, [[UIScreen mainScreen] bounds].size.height-64);
self.scrollView.pagingEnabled = YES;
self.scrollView.contentOffset = CGPointMake([[UIScreen mainScreen] bounds].size.width, 0);

How to bring an image to front in ScrollView

I'm showing images in the ScrollView but I want to know please how can I bring each image to the front in the middle while I'm scrolling. Also it is on the back on each other as in the picture below.
My code:
-(void)scrollView2{
_scrlView.backgroundColor = [UIColor blackColor];
_scrlView.pagingEnabled = NO;
_scrlView.bounces = false;
_scrlView.delegate = self;
_scrlView.showsHorizontalScrollIndicator = YES;
_scrlView.layer.cornerRadius = 2;
_scrlView.contentSize = CGSizeMake(_scrlView.frame.size.width, _scrlView.frame.size.height);
_scrlView.contentSize = CGSizeMake(_scrlView.frame.size.width * 2, _scrlView.frame.size.height);
CGRect screenRect = [[UIScreen mainScreen] bounds];
CGFloat screenWidth = screenRect.size.width;
CGFloat screenHeight = screenRect.size.height;
int segment = screenWidth / 5;
int screenX2 = screenHeight/2 - segment/2;
__block float x = (screenWidth-segment)/2;
NSArray *arrImages=[NSArray arrayWithObjects:#"Smiley-1.png",#"Smiley-2.png",#"Smiley-3.png",#"Smiley-4.png",#"Smiley-5.png", nil];
for(int i=0;i<arrImages.count;i++)
{
UIImageView *imgVw=[[UIImageView alloc]init];
[imgVw setFrame:CGRectMake(x, screenX2, segment, segment)];
[imgVw setImage:[UIImage imageNamed:[NSString stringWithFormat:#"%#",[arrImages objectAtIndex:i]]]];
[imgVw setBackgroundColor:[UIColor redColor]];
[imgVw setContentMode:UIViewContentModeScaleToFill];
[_scrlView addSubview:imgVw];
x=x+segment;
}
[self.view addSubview:_scrlView];
}
float oldY;
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
int scrollX = scrollView.contentOffset.x;
int position = scrollX/segment;
NSLog(#"X: %f , position: %d", scrollView.contentOffset.x , position);
[_scrlView setContentOffset: CGPointMake(_scrlView.contentOffset.x, oldY)];
}
You can use bring sub view to front. For instance
[scrollView bringSubviewToFront:_img1];
I understood what you meant, there is an interesting project. It should help you.
You can also try yourself using a UICollectionView.
- (void)viewDidLoad
{
[super viewDidLoad];
NSArray *arrImages=[NSArray arrayWithObjects:#"smiley1",#"smiley2",#"smiley3",#"smiley4",#"smiley5",#"smiley6", #"smiley7",#"smiley8", nil];
screenRect = [[UIScreen mainScreen] bounds];
screenWidth = screenRect.size.width;
screenHeight = screenRect.size.height;
UIScrollView *scrlView=[[UIScrollView alloc]init];
[scrlView setFrame:CGRectMake(0, 0, 320, 568)];
scrlView.backgroundColor = [UIColor blackColor];
scrlView.pagingEnabled = YES;
scrlView.bounces = false;
scrlView.delegate = self;
scrlView.showsHorizontalScrollIndicator = YES;
scrlView.layer.cornerRadius = 2;
segmentWidth = screenWidth / 5;
long ScrollContentSize = ((arrImages.count -1)*segmentWidth)+screenWidth;
scrlView.contentSize = CGSizeMake(ScrollContentSize, scrlView.frame.size.height);
float screenX2 = screenHeight/2 - segmentWidth/2;
__block float screenX = (screenWidth-segmentWidth)/2;
for(int i=0;i<arrImages.count;i++)
{
UIImageView *imgVw=[[UIImageView alloc]init];
[imgVw setFrame:CGRectMake(screenX, screenX2, segmentWidth+30, segmentWidth+30)];
[imgVw setImage:[UIImage imageNamed:[NSString stringWithFormat:#"%#",[arrImages objectAtIndex:i]]]];
[imgVw setTag:5000+i];
[imgVw setContentMode:UIViewContentModeScaleAspectFit];
screenX = screenX+segmentWidth;
[scrlView addSubview:imgVw];
if (i%2==0)
{
[imgVw setBackgroundColor:[UIColor clearColor]];
[scrlView sendSubviewToBack:imgVw ];
}
else
{
[imgVw setBackgroundColor:[UIColor clearColor]];
[scrlView bringSubviewToFront:imgVw];
}
}
[self.view addSubview:scrlView];
}
Try this

Scrollview not creating through programmatically

I want to creat a scrollview programmatically but the scroll view is not creating the below is my code i want to display images in that scrollview.
fscroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view1.frame.size.width, self.view1.frame.size.height)];
fscroll.contentSize = CGSizeMake(320, 400);
fscroll.backgroundColor = [UIColor blackColor];
fscroll.showsHorizontalScrollIndicator = YES;
[view1 addSubview:fscroll];
int X=0;
for (int i = 0; i < [images count]; i++)
{
imageView = [[UIImageView alloc] initWithFrame: CGRectMake(X, 0, 320, 480)];
imageView.backgroundColor = [UIColor blackColor];
[imageView setImage: [images objectAtIndex:[sender tag]]];
[imageView addSubview:fscroll];
X = X + imageView.frame.size.height;
if(X > 320)
self.fscroll.contentSize = CGSizeMake(X, 134);
if(X > 320)
self.fscroll.contentSize = CGSizeMake(X, 134);
}
X = X + imageView.frame.size.height;
Did you mean to do this?
X = X + imageView.frame.size.width;
EDIT: Also, are you sure view1 has the right frame set before your UIScrollView is initialized?
This is wrong: [imageView addSubview:fscroll]; Presumable you want to do [fscroll addSubview:imageView];
Simple Method: You can created multiple times if you need means
- (void)scrollView
{
int x = 0; //scrollview width
int y = 10;//scrollview height
for(int i=0; i<5; i++)
{
UIScrollView *scrollview=[[UIScrollView alloc]initWithFrame:CGRectMake(x, y, 50, 50)];
scrollview.showsVerticalScrollIndicator=YES;
scrollview.scrollEnabled=YES;
scrollview.userInteractionEnabled=YES;
scrollview.backgroundColor = [UIColor greenColor];
[self.view addSubview:scrollview];
scrollview.contentSize = CGSizeMake(50,50);
x=x+55;
}
}

UIScrollView With Pictures Generated

I have set up a UIScrollView and loaded in pictures and set it equal to the view with some offset between the pictures. Can someone possibly explain what I might have done wrong? It shows the first picture just fine but wont let me scroll left and right to see the next ones. Do I need a gesture recognizer with the new XCode to make this work?
- (void)viewDidLoad
{
[super viewDidLoad];
int PageCount = 3;
UIScrollView *Scroller = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
Scroller.backgroundColor = [UIColor clearColor];
Scroller.pagingEnabled = YES;
Scroller.contentSize = CGSizeMake(PageCount = Scroller.bounds.size.width, Scroller.bounds.size.height);
CGRect ViewSize = Scroller.bounds;
UIImageView *ImgView = [[UIImageView alloc] initWithFrame:ViewSize];
[ImgView setImage:[UIImage imageNamed:#"1.png"]];
[Scroller addSubview:ImgView];
ViewSize = CGRectOffset(ViewSize, Scroller.bounds.size.width, 0);
UIImageView *ImgView2 = [[UIImageView alloc] initWithFrame:ViewSize];
[ImgView2 setImage:[UIImage imageNamed:#"2.png"]];
[Scroller addSubview:ImgView2];
ViewSize = CGRectOffset(ViewSize, Scroller.bounds.size.width, 0);
UIImageView *ImgView3 = [[UIImageView alloc] initWithFrame:ViewSize];
[ImgView3 setImage:[UIImage imageNamed:#"3.png"]];
[Scroller addSubview:ImgView3];
[self.view addSubview:Scroller];
}
#end
You need to manually set the contentSize of your ScrollView to the maximum rectangle which fits all the ImageViews you added. For example : Lets say you want to scroll right and left and you have 4 views with each view having width 100 and you have offset of 10 in x direction. Then after adding all the 4 views to your scrollView, you will have to make the contentSize as below :
scrollView.contentSize = CGSizeMake( 10 + (100 + 10)*4 , scrollView.contentSize.y );
This will make the scrollView scrollable and you will see all the views.
Try this
int PageCount = 3;
NSMutableArray *arrImageName =[[NSMutableArray alloc]initWithObjects:#"1.png",#"2.png",#"3.png", nil];
UIScrollView *Scroller = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
Scroller.scrollEnabled=YES;
Scroller.backgroundColor = [UIColor clearColor];
Scroller.pagingEnabled = YES;
[self.view addSubview:Scroller];
int width=Scroller.frame.size.width;
int xPos=0;
for (int i=0; i<PageCount; i++)
{
UIImageView *ImgView = [[UIImageView alloc]initWithFrame:CGRectMake(xPos, 0, Scroller.frame.size.width, Scroller.frame.size.height)];
[ImgView setImage:[UIImage imageNamed:[arrImageName objectAtIndex:i]]];
[Scroller addSubview:ImgView];
Scroller.contentSize = CGSizeMake(width, 0);
width +=Scroller.frame.size.width;
xPos +=Scroller.frame.size.width;
}

Resources