I am enclosing my sample code herewith for ready reference, please make it in center place, this is what I am facing problem in. It is not stopping on the center indicator.
Note : All button are dynamic and coming from web, but in this sample I made it from a hard coded array. But in original scenerio their count(number of buttons) may change.
- (void)viewDidLoad {
arr = [[NSMutableArray alloc] initWithObjects:#"Test1",#"Test2",#"Test3",#"Test4",#"Test5",nil];
UIImageView *img = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"bg_content_slide.png"]];
img.frame = CGRectMake(0, 20, 320, 85);
img.contentMode = UIViewContentModeScaleAspectFill;
[self.view addSubview:img];
[self setScrollButtons];
[super viewDidLoad];
}
- (void) setScrollButtons
{
UIScrollView *tagScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 30, 320, 44)];
tagScrollView.backgroundColor = [UIColor clearColor];
[tagScrollView setShowsHorizontalScrollIndicator:NO];
[tagScrollView setShowsVerticalScrollIndicator:NO];
[tagScrollView setCanCancelContentTouches:NO];
tagScrollView.indicatorStyle = UIScrollViewIndicatorStyleBlack;
tagScrollView.clipsToBounds = YES;
tagScrollView.scrollEnabled = YES;
tagScrollView.pagingEnabled = YES;
tagScrollView.bounces = YES;
tagScrollView.tag = 2;
int xaxis = 0;
for (int i = 0; i < [arr count]; i++) {
UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn.frame = CGRectMake(xaxis, 0, 160, 44);
//btn.titleLabel.text = [NSString stringWithFormat:#"%#",[arr objectAtIndex:i]];
[btn setTitle:[arr objectAtIndex:i] forState:UIControlStateNormal];
NSLog(#"%#", btn.titleLabel.text);
xaxis += btn.frame.size.width + 20;
[tagScrollView addSubview:btn];
}
[tagScrollView setContentSize:CGSizeMake([arr count] * 200, tagScrollView.frame.size.height)];
[self.view addSubview:tagScrollView];
}
Find code here UnAligned scrollview code
chage ur code with this one
int xaxis = 0;
for (int i = 0; i < [arr count]; i++) {
UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn.frame = CGRectMake(320*xaxis+80, 0, 160, 44);
//btn.titleLabel.text = [NSString stringWithFormat:#"%#",[arr objectAtIndex:i]];
[btn setTitle:[arr objectAtIndex:i] forState:UIControlStateNormal];
//[btn setBackgroundImage:[UIImage imageNamed:#"remove_fav.png"] forState:UIControlStateNormal];
NSLog(#"%#", btn.titleLabel.text);
xaxis ++;//= btn.frame.size.width + 40;
[tagScrollView addSubview:btn];
}
[tagScrollView setContentSize:CGSizeMake([arr count] * 320, tagScrollView.frame.size.height)];
[self.view addSubview:tagScrollView];
it will solve ur problem
What you want to achieve is not possible when pagination is enabled as when pagination is enabled the scroll view scroll to its width.So for this what you can do max is add this method
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
CGPoint offset = scrollView.contentOffset;
int temp = floor(offset.x/160);
[tagScrollView scrollRectToVisible:CGRectMake(temp*160, 0, 320, 44) animated:YES];
}
its working fine for this configuration .......
{
int xaxis = 0;
for (int i = 0; i < [arr count]; i++) {
UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn.frame = CGRectMake(160*xaxis+85, 0, 150, 44);
[btn setTitle:[arr objectAtIndex:i] forState:UIControlStateNormal];
NSLog(#"%#", btn.titleLabel.text);
xaxis ++;//= btn.frame.size.width + 40;
[tagScrollView addSubview:btn];
}
[tagScrollView setContentSize:CGSizeMake(160*xaxis+250, tagScrollView.frame.size.height)];
}
and .....
tagScrollView.pagingEnabled = NO;
Related
Like suppose when i scroll the gallery the button which comes in the middle will shift 5px up than the other So whatever button comes up in the middle it will be little up than other. So please help me
scrlview=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 350, 320, 130)];
[scrlview setContentSize:CGSizeMake(600, 0)];
scrlview.delegate=self;
scrlview.backgroundColor=[UIColor blueColor];
scrlview.scrollsToTop=NO;
[self.view addSubview:scrlview];
btn1=[UIButton buttonWithType:UIButtonTypeRoundedRect];
btn1.frame=CGRectMake(10, 10, 55, 50);
[btn1 setTitle:#"Btn1" forState:UIControlStateNormal];
[scrlview addSubview:btn1];
btn2=[UIButton buttonWithType:UIButtonTypeRoundedRect];
btn2.frame=CGRectMake(70, 10, 55, 50);
[btn2 setTitle:#"Btn2" forState:UIControlStateNormal];
[scrlview addSubview:btn2];
btn3=[UIButton buttonWithType:UIButtonTypeRoundedRect];
btn3.frame=CGRectMake(130, 10, 55, 50);
[btn3 setTitle:#"Btn3" forState:UIControlStateNormal];
[scrlview addSubview:btn3];
btn4=[UIButton buttonWithType:UIButtonTypeRoundedRect];
btn4.frame=CGRectMake(190, 10, 55, 50);
[btn4 setTitle:#"Btn4" forState:UIControlStateNormal];
[scrlview addSubview:btn4];
btn5=[UIButton buttonWithType:UIButtonTypeRoundedRect];
btn5.frame=CGRectMake(250, 10, 55, 50);
[btn5 setTitle:#"Btn5" forState:UIControlStateNormal];
[scrlview addSubview:btn5];
btn6=[UIButton buttonWithType:UIButtonTypeRoundedRect];
btn6.frame=CGRectMake(310, 10, 55, 50);
[btn6 setTitle:#"Btn6" forState:UIControlStateNormal];
[scrlview addSubview:btn6];
btn7=[UIButton buttonWithType:UIButtonTypeRoundedRect];
btn7.frame=CGRectMake(370, 10, 55, 50);
[btn7 setTitle:#"Btn7" forState:UIControlStateNormal];
[scrlview addSubview:btn7];
btn8=[UIButton buttonWithType:UIButtonTypeRoundedRect];
btn8.frame=CGRectMake(430, 10, 55, 50);
[btn8 setTitle:#"Btn8" forState:UIControlStateNormal];
[scrlview addSubview:btn8];
btn9=[UIButton buttonWithType:UIButtonTypeRoundedRect];
btn9.frame=CGRectMake(490,10, 55, 50);
[btn9 setTitle:#"Btn9" forState:UIControlStateNormal];
[scrlview addSubview:btn9];
I have tried out your requirement. I have assumed that the offset between buttons and their width is same.
- (void)viewDidLoad
{
[super viewDidLoad];
self.scrollView.delegate = self;
[self addButtonsToScrollView];
}
- (void)addButtonsToScrollView
{
NSInteger numberOfButtons = 15;
//NSInteger visibleButtons = 5;
//CGFloat totalWidth = self.scrollView.frame.size.width;
CGFloat offset = 5.0f;
CGFloat buttonWidth = 54.0f;//(totalWidth - ((visibleButtons+1)*offset))/visibleButtons;
CGFloat buttonHeight = 44.0f;
CGPoint origin = CGPointMake(offset, 10.0f);
for (int idx = 1; idx<=numberOfButtons; idx++) {
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
CGRect buttonFrame = CGRectZero;
buttonFrame.origin = origin;
buttonFrame.size = CGSizeMake(buttonWidth, buttonHeight);
button.frame = buttonFrame;
NSString *titleString = [NSString stringWithFormat:#"Title%d",idx+1];
[button setTitle:titleString forState:UIControlStateNormal];
button.tag = idx;
[self.scrollView addSubview:button];
origin.x+=buttonWidth+offset;
}
[self.scrollView setContentSize:CGSizeMake(origin.x, 200.0f)];
}
- (void)adjustCenterButton
{
//content point is set in such a way that there would be exaclty 5 button visible all the time
CGPoint contentOffset = self.scrollView.contentOffset;
NSUInteger units = floorf(contentOffset.x/59.0f); //buttowidth+offset
contentOffset.x = units*59.0f;
[self.scrollView setContentOffset:contentOffset animated:YES];
NSArray *subViews = [self.scrollView subviews];
//Finds the tag of button which would be the center button
//since the visible buttons are 5, 3 is added,
NSInteger selecteButtonIndex = floorf(contentOffset.x/(59.0f))+3;
for (UIView *button in subViews)
{
//Just a bypass to remove the scroll indicator
if (button.tag!=0)
{
CGRect frame = button.frame;
frame.origin.y = (button.tag == selecteButtonIndex)?5.0f:10.0f;
button.frame = frame;
}
}
CGSize contentSize = self.scrollView.contentSize;
contentSize.height = 200.0f;//self.scrollView.frame.size.width;
self.scrollView.contentSize = contentSize;
}
#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
/*The update is only called when the scrollView is not decelerating.
The method is an required if the user is scrolling very slowly,
the other scrollViewDidEndDecelerating: will not be invoked*/
if(!decelerate)
{
[self adjustCenterButton];
}
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
[self adjustCenterButton];
}
You can use of loop like for loop or while loop.
Let me give you some idea about that.
Here i have updated answer with sample code with more clearly
NSArray *yourArray = [[NSArray alloc] initWithObjects:#"btn1",#"btn2",#"btn3",#"btn4",#"btn5",#"btn6",#"btn7", nil];
int xPos = 10;
for (int i = 0 ; i < yourArray.count; i ++) {
int yPos = 30;
if (i == (yourArray.count /2)) {
yPos = 25;
}
UIButton *button =[UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame=CGRectMake(xPos, yPos, 55, 50);
button.tag = i;
[button setTitle:[yourArray objectAtIndex:i] forState:UIControlStateNormal];
[scrollView addSubview:button];
xPos+=55;
}
[scrollView setContentSize:CGSizeMake(xPos+10, 55)];
// To get Center Button
UIButton *centerButton = (UIButton *)[scrlview viewWithTag:(yourArray.count /2)];
May be this will be help you.
All the best !!!
Try this,
for(UIButton *subview in yourScrollView.subviews)
{
CGRect subviewRect = [yourScrollView convertRect:subview.frame fromView:subview];
if(CGRectContainsPoint(subviewRect,middlePointOfScrollView))
{
// change y position of button
break;
}
}
I am trying to parse the text of particular label from list of label showed in UIScrollView into next view show in label. But these label text got from NSMutableArray. i tried but i got only last value of the NSMutableArray See following code for Ref: Plz suggest what i will do
-(void)dataPrinting
{
int t= 60;
NSLog(#"%#",totalData);
for (int i = 0; i < [totalData count]; i++)
{
pplname=[[UILabel alloc]initWithFrame:CGRectMake(10, t, 200, 30)];
pplname.backgroundColor=[UIColor clearColor];
pplname.text=[totalData objectAtIndex:i];
pplname.textColor=[UIColor redColor];
[scrollView addSubview:pplname];
UIButton *button=[UIButton buttonWithType:UIButtonTypeCustom];
//initWithFrame:CGRectMake(10, t, 200, 40) ];
button.frame=CGRectMake(10, t, 200, 40);
[button addTarget:self
action:#selector(nextview)
forControlEvents:UIControlEventTouchUpInside];
[scrollView addSubview:button];
t=t+40;
}
[scrollView setContentSize:CGSizeMake(scrollView.frame.size.width, t+2)];
[aview stopAnimating];
}
-(IBAction)nextview
{
Details *detailsPage=[[Details alloc]initWithNibName:#"Details" bundle:nil];
for (int i = 0; i < [totalData count]; i++)
{
detailsPage.pplName=[totalData objectAtIndex:i];
}
[self presentModalViewController:detailsPage animated:YES];
}
Give the tag to fetch particular name of label just replace my bellow code with your code...
-(void)dataPrinting
{
int t= 60;
NSLog(#"%#",totalData);
for (int i = 0; i < [totalData count]; i++)
{
pplname=[[UILabel alloc]initWithFrame:CGRectMake(10, t, 200, 30)];
pplname.backgroundColor=[UIColor clearColor];
pplname.text=[totalData objectAtIndex:i];
pplname.tag = i;
pplname.textColor=[UIColor redColor];
[scrollView addSubview:pplname];
UIButton *button=[UIButton buttonWithType:UIButtonTypeCustom];
//initWithFrame:CGRectMake(10, t, 200, 40) ];
button.frame=CGRectMake(10, t, 200, 40);
button.tag = i;
[button addTarget:self
action:#selector(nextview)
forControlEvents:UIControlEventTouchUpInside];
[scrollView addSubview:button];
t=t+40;
}
[scrollView setContentSize:CGSizeMake(scrollView.frame.size.width, t+2)];
[aview stopAnimating];
}
-(IBAction)nextview:(id)sender
{
UIButton *btnTemp = (UIButton*)sender;
Details *detailsPage=[[Details alloc]initWithNibName:#"Details" bundle:nil];
detailsPage.pplName=[totalData objectAtIndex:btnTemp.tag];
[self presentModalViewController:detailsPage animated:YES];
}
I want create 32 views like above view. so that what i modify in my code so i get this view? I shown code below
- (void)loadView {
[super loadView];
self.view.backgroundColor = [UIColor redColor];
UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
scroll.pagingEnabled = YES;
NSInteger numberOfViews = 33;
[btnMenu setTag:0 ];
for (int i = 1; i < numberOfViews; i++) {
CGFloat yOrigin = i * self.view.frame.size.width;
UIView *awesomeView = [[UIView alloc] initWithFrame:CGRectMake(yOrigin, 0, self.view.frame.size.width, self.view.frame.size.height)];
btnMenu = [UIButton buttonWithType:UIButtonTypeCustom];
//NSData *data =UIImageJPEGRepresentation(, 1);
[btnMenu setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:#"page-%d.jpg",i]] forState:UIControlStateNormal];
CGRect frame = btnMenu.frame;
frame.size.width=320;
frame.size.height=420;
frame.origin.x=0;
frame.origin.y=0;
btnMenu.frame=frame;
[btnMenu setTag:i];
btnMenu.alpha = 1;
[btnMenu addTarget:self action:#selector(btnSelected:) forControlEvents:UIControlEventTouchUpInside];
[awesomeView addSubview:btnMenu];
[scroll addSubview:awesomeView];
[awesomeView release];
}
scroll.contentSize = CGSizeMake(self.view.frame.size.width * numberOfViews, self.view.frame.size.height);
[self.view addSubview:scroll];
[scroll release];}
-(IBAction)btnSelected:(id)sender{
UIButton *button = (UIButton *)sender;
int whichButton = button.tag;
NSLog(#"Current TAG: %i", whichButton);
if(whichButton==1)
{
first=[[FirstImage alloc]init];
[self.navigationController pushViewController:first animated:YES];
}
}
scroll.contentSize = CGSizeMake(self.view.frame.size.width * numberOfViews, self.view.frame.size.height);
write this line in your code before the for loop.
In this code I want to do changes like as instead of button I want to create images in image view with scroll view with zoom in and zoom out event on image. And two or three button on every image view so that I can implement some event on button. How do that?
- (void)loadView {
[super loadView];
self.view.backgroundColor = [UIColor redColor];
UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
scroll.pagingEnabled = YES;
NSInteger numberOfViews = 33;
[btnMenu setTag:0 ];
for (int i = 1; i < numberOfViews; i++) {
CGFloat yOrigin = i * self.view.frame.size.width;
UIView *awesomeView = [[UIView alloc] initWithFrame:CGRectMake(yOrigin, 0, self.view.frame.size.width, self.view.frame.size.height)];
//awesomeView.backgroundColor = [UIColor colorWithRed:0.5/i green:0.5 blue:0.5 alpha:1];
btnMenu = [UIButton buttonWithType:UIButtonTypeCustom];
//NSData *data =UIImageJPEGRepresentation(, 1);
[btnMenu setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:#"page-%d.jpg",i]] forState:UIControlStateNormal];
CGRect frame = btnMenu.frame;
frame.size.width=320;
frame.size.height=460;
frame.origin.x=0;
frame.origin.y=0;
btnMenu.frame=frame;
[btnMenu setTag:i];
btnMenu.alpha = 1;
[btnMenu addTarget:self action:#selector(btnSelected:) forControlEvents:UIControlEventTouchUpInside];
[awesomeView addSubview:btnMenu];
[scroll addSubview:awesomeView];
[awesomeView release];
}
scroll.contentSize = CGSizeMake(self.view.frame.size.width * numberOfViews, self.view.frame.size.height);
[self.view addSubview:scroll];
[scroll release];
}
-(IBAction)btnSelected:(id)sender{
UIButton *button = (UIButton *)sender;
int whichButton = button.tag;
NSLog(#"Current TAG: %i", whichButton);
if(whichButton==1)
{
first=[[FirstImage alloc]init];
[self.navigationController pushViewController:first animated:YES];
}
}
You need Image instead of Button right? Then Try This:
scroll=[[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];
scroll.backgroundColor=[UIColor clearColor];
scroll.pagingEnabled=YES;
scroll.contentSize = CGSizeMake(320*33, 300);
CGFloat x=0;
for(int i=1;i<34;i++)
{
UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(x+0, 0, 320, 460)];
[image setImage:[UIImage imageNamed:[NSString stringWithFormat:#"page-%d.jpg",i]]];
[scroll addSubview:image];
[image release];
x+=320;
}
[self.view addSubview:scroll];
scroll.showsHorizontalScrollIndicator=NO;
[scroll release];
I am trying to create a scrollable menu bar (which I am succeeding in doing) but now instead I want to add a large button with an image centred in the button, instead of taking up the total width and height of the button
THis is my current code:
[scrollView setBackgroundColor:[UIColor whiteColor]];
[scrollView setCanCancelContentTouches:NO];
scrollView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
scrollView.clipsToBounds = YES;
scrollView.scrollEnabled = YES;
UIImage *image;
UIImageView *iv;
UIButton *button;
count = [returned count];
for (int i=0; i<count; i++)
{
image = [UIImage imageNamed:#"coat2.jpg"];
iv = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,105,120)];
button = [[UIButton alloc] initWithFrame:CGRectMake(0,0,105,120)];
[button setAlpha:100];
[button setTag:1];
[button addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
iv.image = image;
iv.tag=#"coat2.jpg";
iv.userInteractionEnabled = YES;
[iv insertSubview:button atIndex:0];
[button release];
[scrollView addSubview:iv];
[iv release];
}
[scrollView setScrollEnabled:YES];
[self layoutScrollImages];
scrollView.delegate = self;
the above code creates an image and button over it where the button and image are of the exact same width and height. I want the button to be significantly larger both in height and width.
Is this possible?
Thanks!
2nd attempt (using help from Joseph Tura)
[scrollView setBackgroundColor:[UIColor whiteColor]];
[scrollView setCanCancelContentTouches:NO];
scrollView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
scrollView.clipsToBounds = YES;
scrollView.scrollEnabled = YES;
UIImage *image;
count = [returned count];
for (int i=0; i<count; i++)
{
image = [UIImage imageNamed:#"coat2.jpg"];
iv = [[UIImageView alloc] initWithFrame:CGRectMake(0,20,105,120)];
button = [[ProductButton alloc] initWithFrame:CGRectMake(0,20,105,120)];
[button setAlpha:100];
[button setTag:1];
[button addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
iv.tag=tag;
iv.userInteractionEnabled = YES;
iv.image = myimage;
UIView *uiView = [[UIView alloc] initWithFrame:CGRectMake(0,20,105,120)];
[uiView addSubview:iv];
button = [[ProductButton alloc] initWithFrame:CGRectMake(0,0,210,240)];
button.center = CGPointMake(uiView.frame.size.width /2, uiView.frame.size.height/2);
[button setTag:1];
[button addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
[uiView addSubview:button];
[scrollView addSubview:uiView];
}
[scrollView setScrollEnabled:YES];
[self layoutScrollImages];
scrollView.delegate = self;
-----
-(void)layoutScrollImages
{
//UIImageView *view = nil;
UIView *view = nil;
NSArray *subviews = [scrollView subviews];
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 += 110;
}
}
[scrollView setContentSize: CGSizeMake(count*110, [scrollView bounds].size.height)];
}
Why not put both elements inside a UIView?
UIView *aView = [[UIView alloc] initWithFrame:iv.frame];
[aView addSubview:iv];
button = [[UIButton alloc] initWithFrame:CGRectMake(0,0,210,240)];
button.center = CGPointMake(aView.frame.size.width / 2, aView.frame.size.height / 2);
[aView addSubview:button];