uibutton is not getting displayed - ios

I am trying to add uibutton inside the imageview.
But it was not displayed the button.
Even i tried adding it to the uiscrollview and also for self.view.
But nothing were displayed the uibutton
Pls let me know what is the problem
const CGFloat HEIGHT = 1024.0;
const CGFloat WIDTH = 768.0;
#define myViewPortrait CGRectMake(0.0f, 0.0f, 768.0f,1024.0f)
#define myViewLandSacpe CGRectMake(0.0f, 0.0f, 1024.0f,768.0f)
#define kAnimationKey #"animationKey"
-(void)layoutScrollImages
{
UIImageView *view = nil;
NSArray *subviews = [myScrollView subviews];
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 += (self.view.frame.size.width);
}
}
[myScrollView setContentSize:CGSizeMake((myImagesCount * self.view.frame.size.width), [myScrollView bounds].size.height)];
}
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
self.view.backgroundColor = [UIColor viewFlipsideBackgroundColor];
myScrollView = [[UIScrollView alloc] initWithFrame:
CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
[self.view addSubview:myScrollView];
UISwipeGestureRecognizer *rightRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(rightSwipeHandle:)];
rightRecognizer.direction = UISwipeGestureRecognizerDirectionRight;
rightRecognizer.numberOfTouchesRequired = 1;
[rightRecognizer setDelegate:self];
[myScrollView addGestureRecognizer:rightRecognizer];
[rightRecognizer release];
UISwipeGestureRecognizer *leftRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(leftSwipeHandle:)];
leftRecognizer.direction = UISwipeGestureRecognizerDirectionLeft;
leftRecognizer.numberOfTouchesRequired = 1;
[leftRecognizer setDelegate:self];
[myScrollView addGestureRecognizer:leftRecognizer];
[leftRecognizer release];
[myScrollView setBackgroundColor:[UIColor blackColor]];
[myScrollView setCanCancelContentTouches:NO];
myScrollView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
myScrollView.clipsToBounds = YES;
myScrollView.scrollEnabled = YES;
myScrollView.pagingEnabled = YES;
myScrollView.delegate = self;
myImagesCount = 5;
myScrollView.showsHorizontalScrollIndicator=NO;
myScrollView.showsVerticalScrollIndicator=NO;
for (int i = 1; i <= myImagesCount; i++)
{
NSString *imageName = [NSString stringWithFormat:#"screen-%d.jpg", i];
UIImage *image = [UIImage imageNamed:imageName];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
CGRect rect = imageView.frame;
rect.size.height = myScrollView.frame.size.height;
NSLog(#"%d -----",self.view.frame.size.width);
rect.size.width = myScrollView.frame.size.width;
imageView.frame = rect;
imageView.tag = i;
[myScrollView addSubview:imageView];
[imageView release];
}
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self action:#selector(buttonHandler) forControlEvents:UIControlEventAllEvents];
[button setTitle:#"point" forState:UIControlStateNormal];
button.frame = CGRectMake(0.0, 0.0, 100.0, 40.0);
button.backgroundColor = [UIColor blackColor];
[self.view addSubview:button];
[self layoutScrollImages];
[super viewDidLoad];
}

Based off your question, you'd like to add the UIButton as a subview to the UIImageView - I'm guessing in the for loop? Immediate problem I see is that the actual button is being generated outside of the for loop. I think your for loop is intended to look like this:
for (int i = 1; i <= myImagesCount; i++)
{
NSString *imageName = [NSString stringWithFormat:#"screen-%d.jpg", i];
UIImage *image = [UIImage imageNamed:imageName];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self action:#selector(buttonHandler) forControlEvents:UIControlEventAllEvents];
[button setTitle:#"point" forState:UIControlStateNormal];
button.frame = CGRectMake(0.0, 0.0, 100.0, 40.0);
button.backgroundColor = [UIColor blackColor];
CGRect rect = imageView.frame;
rect.size.height = myScrollView.frame.size.height;
NSLog(#"%d -----",self.view.frame.size.width);
rect.size.width = myScrollView.frame.size.width;
imageView.frame = rect;
imageView.tag = i;
[imageView addSubview:button];
[myScrollView addSubview:imageView];
[imageView release];
}

Try this
Declare button globally and set
[self layoutScrollImages];
[self.view bringSubViewToFront:button];

Related

how to get current image tag in scroll view?

//add the scrollview to the view
Friend_Request_Scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(self.view.frame.size.width/2-60,70,120,140)];
[Friend_Request_Scroll setAlwaysBounceVertical:NO];
Friend_Request_Scroll.delegate=self;
Friend_Request_Scroll.pagingEnabled=YES;
[Friend_Request_Scroll setShowsHorizontalScrollIndicator:NO];
[Friend_Request_Scroll setShowsVerticalScrollIndicator:NO];
CGFloat xOrigin = 0;
for (int i = 0; i <[imageCollection count]; i++) {
Back_Request_Scroll=[[UIView alloc]init];
Back_Request_Scroll.frame=CGRectMake(xOrigin,Friend_Request_Scroll.frame.origin.y-135,120,140);
Back_Request_Scroll.backgroundColor=[UIColor colorWithRed:244.0f/255.0f green:244.0f/255.0f blue:244.0f/255.0f alpha:0.5];
Back_Request_Scroll.clipsToBounds=YES;
[Friend_Request_Scroll addSubview:Back_Request_Scroll];
image = [[UIImageView alloc] initWithFrame:
CGRectMake(0, 0,
Back_Request_Scroll.frame.size.width,
Back_Request_Scroll.frame.size.height)];
[image sd_setImageWithURL:[imageCollection objectAtIndex:i] placeholderImage:[UIImage imageNamed:#""]];
image.tag=i;
[Back_Request_Scroll addSubview:image];
xOrigin= xOrigin+120;
image.userInteractionEnabled=YES;
}
Friend_Request_Scroll.contentSize = CGSizeMake(xOrigin,
0);
[self.view addSubview:Friend_Request_Scroll];
swipeleft=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:#selector(swipeleft:)];
swipeleft.direction=UISwipeGestureRecognizerDirectionLeft;
[image addGestureRecognizer:swipeleft];
swiperight=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:#selector(swiperight:)];
swiperight.direction=UISwipeGestureRecognizerDirectionRight;
[image addGestureRecognizer:swiperight];
pageControl = [[UIPageControl alloc] init];
pageControl.frame = CGRectMake(self.view.frame.size.width/2-60,Friend_Request_Scroll.frame.size.height+Friend_Request_Scroll.frame.origin.y,image.frame.size.width,20);
pageControl.numberOfPages = [imageCollection count];
pageControl.currentPage = currentValue;
[pageControl addTarget:self action:#selector(changePage) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:pageControl];
Try this Delegate Method.This delegate will tell you that scrolling has finished.
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
Try this code, you use recongizer object view tag.
- (void)viewDidLoad
{
[super viewDidLoad];
scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
[self.view addSubview:scroll];
NSInteger i;
for (i=0; i<20; i++)
{
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, i*90 + i*15, 300, 90)];
imageView.backgroundColor = [UIColor blackColor];
imageView.userInteractionEnabled = YES;
imageView.tag = i;
NSLog(#“image Tag = %d”, imageView.tag);
[scroll addSubview:imageView];
UISwipeGestureRecognizer *recognizer;
recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleSwipeFrom:)];
[recognizer setDirection:(UISwipeGestureRecognizerDirectionRight | UISwipeGestureRecognizerDirectionDown | UISwipeGestureRecognizerDirectionLeft | UISwipeGestureRecognizerDirectionUp)];
[imageView addGestureRecognizer: recognizer];
}
scroll.contentSize = CGSizeMake(320, 110*i);
}
-(void)handleSwipeFrom:(UISwipeGestureRecognizer *)recognizer {
NSLog(#"Swipe received.");
NSLog(#“image Tag = %d”, recognizer.view.tag);
}
Update
No need swipe.
- (void)scrollViewDidScroll:(UIScrollView *)sender {
if (sender == scroll) {
int pageNum = (int)(scroll.contentOffset.x / scroll.frame.size.width);
NSLog(#"%d",pageNum);
//self.pagecontroller.currentPage =pageNum;
}
}
- (void)viewDidLoad {
[super viewDidLoad];
scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, self.view.frame.size.height)];
scroll.delegate = self;
scroll.pagingEnabled = YES;
[self.view addSubview:scroll];
CGFloat currentXOffset = 0;
NSInteger i;
for (i=0; i<3; i++)
{
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(currentXOffset, 100, self.view.frame.size.width, 200)];
if (i == 0){
imageView.backgroundColor = [UIColor blackColor];
}else if (i == 1){
imageView.backgroundColor = [UIColor orangeColor];
}else if (i == 2){
imageView.backgroundColor = [UIColor grayColor];
}
imageView.userInteractionEnabled = YES;
imageView.tag = i;
currentXOffset = currentXOffset + imageView.frame.size.width;
[scroll addSubview:imageView];
// UISwipeGestureRecognizer *recognizer;
//
// recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleSwipeFrom:)];
// [recognizer setDirection:(UISwipeGestureRecognizerDirectionRight | UISwipeGestureRecognizerDirectionDown | UISwipeGestureRecognizerDirectionLeft | UISwipeGestureRecognizerDirectionUp)];
// [imageView addGestureRecognizer: recognizer];
}
scroll.contentSize = CGSizeMake(currentXOffset, self.view.frame.size.height);
}
Add tap gesture action to the image and you can get the tag using [sender tag]
I have Few ImageView each of them have tag and I have an array of images. when user tap on image then I get image tag and take action on it.
- (void)viewDidLoad
{
[super viewDidLoad];
scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
[self.view addSubview:scroll];
NSInteger i;
for (i=0; i<20; i++)
{
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, i*90 + i*15, 300, 90)];
imageView.backgroundColor = [UIColor blackColor];
imageView.userInteractionEnabled = YES;
imageView.tag = i;
NSLog(#“image Tag = %d”, imageView.tag);
[scroll addSubview:imageView];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(findTheTag:)];
[imageView addGestureRecognizer:tap];
}
scroll.contentSize = CGSizeMake(320, 110*i);
}
- (void)findTheTag:(id)sender
{
NSLog(#“image Tag = %d”, sender.tag);
}

ios : how to add uiLabel on top of the background image?

I am trying to add the uiLabel on top of the uiImageVIew which is the background image. When it comes to adding the uiLabel on top of the uiImageView, the uilabel cannot be aded when testing and running. Would you please tell me the alternatives or precautions if doing so ?
The below is my code :
#implementation CoverViewController
- (void)viewDidLoad {
[super viewDidLoad];
_container = [[UIView alloc] initWithFrame:[self.view bounds]];
[self setTapDemo :[UIImage imageNamed:#"cover_1_d.png"] ] ;
}
- (void) setTapDemo: (UIImage *) cover {
_image = [UIImage imageNamed:#"shaded_region.png"];
_imageShaded = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
[_imageShaded setImage:_image];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
[imageView setImage: cover ];
_imageShaded.userInteractionEnabled = YES;
UITapGestureRecognizer *myGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:#selector(touchesBegan:)];
myGesture.numberOfTapsRequired = 1;
myGesture.delegate=self;
[_imageShaded addGestureRecognizer:myGesture];
[_imageShaded setContentMode:UIViewContentModeScaleAspectFill];
NSString * text = #"Quad name:";
CGSize labelSize = CGSizeMake(60, CGFLOAT_MAX);
UILabel *fromLabel = [[UILabel alloc]initWithFrame:CGRectMake(91, 15, labelSize.width, labelSize.height)];
fromLabel.text = text;
fromLabel.numberOfLines = 1;
fromLabel.lineBreakMode = NSLineBreakByWordWrapping;
fromLabel.baselineAdjustment = UIBaselineAdjustmentAlignBaselines; // or UIBaselineAdjustmentAlignCenters, or UIBaselineAdjustmentNone
fromLabel.adjustsFontSizeToFitWidth = YES;
fromLabel.minimumScaleFactor = 10.0f/12.0f;
fromLabel.clipsToBounds = YES;
fromLabel.backgroundColor = [UIColor clearColor];
fromLabel.textAlignment = NSTextAlignmentLeft;
[_imageShaded addSubview:fromLabel];
[imageView addSubview:fromLabel];
[_container addSubview:_imageShaded];
[_container addSubview:imageView];
[_container addSubview:fromLabel];
[self.view addSubview:_container];
[self.view sendSubviewToBack:_container];
}
Check This
- (void) setTapDemo: (UIImage *) cover {
_image = [UIImage imageNamed:#"images.jpg"];
_imageShaded = [[UIImageView alloc] initWithFrame:CGRectMake(0, 60, self.view.frame.size.width, self.view.frame.size.height)];
[_imageShaded setImage:_image];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 60, self.view.frame.size.width, self.view.frame.size.height)];
[imageView setImage: cover ];
_imageShaded.userInteractionEnabled = YES;
UITapGestureRecognizer *myGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:#selector(touchesBegan:)];
myGesture.numberOfTapsRequired = 1;
myGesture.delegate=self;
[_imageShaded addGestureRecognizer:myGesture];
[_imageShaded setContentMode:UIViewContentModeScaleAspectFill];
NSString * text = #"Quad name:";
// CGSize labelSize = CGSizeMake(60, CGFLOAT_MAX);
UILabel *fromLabel = [[UILabel alloc]initWithFrame:CGRectMake(91, 15, 300, 40)];
fromLabel.text = text;
fromLabel.textColor = [UIColor greenColor];
fromLabel.numberOfLines = 1;
fromLabel.lineBreakMode = NSLineBreakByWordWrapping;
fromLabel.baselineAdjustment = UIBaselineAdjustmentAlignBaselines; // or UIBaselineAdjustmentAlignCenters, or UIBaselineAdjustmentNone
fromLabel.adjustsFontSizeToFitWidth = YES;
fromLabel.minimumScaleFactor = 10.0f/12.0f;
fromLabel.clipsToBounds = YES;
fromLabel.backgroundColor = [UIColor clearColor];
fromLabel.textAlignment = NSTextAlignmentLeft;
[_container addSubview:_imageShaded];
[_container addSubview:imageView];
[self.view addSubview:_container];
[self.view sendSubviewToBack:_container];
[imageView addSubview:fromLabel];
}
You are adding your UILabel more than once on different views. you should add it on a single view at a time;
//[_imageShaded addSubview:fromLabel]; // 1
//[imageView addSubview:fromLabel]; // 2
[_container addSubview:_imageShaded];
[_container addSubview:imageView];
//[_container addSubview:fromLabel]; //3

UIScrollView not scrolling iOS

I am trying to create several UIScrollViews in a single view that scroll horizontally in iOS. This is my code so far:
-(void)updateSection {
[feedLoadingActInd stopAnimating];
feedLoadingActInd.hidden = YES;
builder = [NSMutableArray array];
float xPosition = 0;
float xPosBut = 0;
scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 400, self.frame.size.width, self.frame.size.height - 29)];
[scrollView setScrollEnabled:YES];
scrollView.backgroundColor = [UIColor yellowColor];
scrollView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
scrollView.pagingEnabled = YES;
scrollView.delegate = self;
for (int i = 0; i < itemArticleArray.count; i++) {
testButton = [[UIButton alloc] initWithFrame:CGRectMake(xPosBut, 40, 40, 40)];
[testButton setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
[testButton setTitle:#"Test" forState:UIControlStateNormal];
[testButton addTarget:self action:#selector(buttonPressed) forControlEvents:UIControlEventTouchUpInside];
testButton.backgroundColor = [UIColor blueColor];
xPosBut += testButton.frame.size.width;
NSLog(#"scroll.frame.size.width = %f", scrollView.frame.size.width);
xPosition += 2;
UIView *seperatorView = [[UIView alloc] initWithFrame:CGRectMake(xPosition, 4, 350, scrollView.frame.size.height - 8)];
seperatorView.backgroundColor = [UIColor redColor];
[scrollView addSubview:seperatorView];
xPosition += 350;
[seperatorView addSubview:testButton];
[scrollView addSubview:seperatorView];
[builder addObject:testButton];
}
[self addSubview:scrollView];
[scrollView setUserInteractionEnabled:YES];
[scrollView setContentSize: CGSizeMake(xPosition, scrollView.frame.size.height)];
NSLog(#"scroll.contentsize.width = %f", scrollView.contentSize.width);
}
However, none of the scroll views are actually scrolling, which I am confused about, as there are multiple buttons being added. Also, the buttons that I have added, are not actually doing anything when I press them. They should be running the buttonPressed method, and it doesn't?
Any help would be muchly appreciated!
try this,
float xPosition = 0;
float xPosBut = 0;
scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 100,self.view.frame.size.width, self.view.frame.size.height - 29)];
[scrollView setScrollEnabled:YES];
scrollView.backgroundColor = [UIColor yellowColor];
for (int i = 0; i < 10; i++) {
UIButton *testButton = [[UIButton alloc] initWithFrame:CGRectMake(xPosition, 10, scrollView.frame.size.width, 50)];
[testButton setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
[testButton setTitle:#"Test" forState:UIControlStateNormal];
[testButton addTarget:self action:#selector(buttonPressed) forControlEvents:UIControlEventTouchUpInside];
testButton.backgroundColor = [UIColor blueColor];
xPosition += testButton.frame.size.width;
NSLog(#"xposbut = %f", xPosBut);
NSLog(#"scroll.frame.size.width = %f", scrollView.frame.size.width);
xPosition += scrollView.frame.size.width+2;
UIView *seperatorView = [[UIView alloc] initWithFrame:CGRectMake(xPosition, 4, 2, scrollView.frame.size.height - 8)];
seperatorView.backgroundColor = [UIColor redColor];
[scrollView addSubview:seperatorView];
xPosition +=scrollView.frame.size.width+ 4;
[self.view addSubview:scrollView];
[scrollView addSubview:seperatorView];
[scrollView addSubview:testButton];
[builder addObject:testButton];
}
[scrollView setContentSize: CGSizeMake(xPosition, scrollView.frame.size.height)];
[self.view addSubview:scrollView];
set the for loop condition part as per your requirement.
hope this will help you.

UIScrollView add zoom for the self.view

I need zooming all objects in the view, but lose the position of the objects (UIButton)
- (void)viewDidLoad
{
CGRect scrollViewFrame = CGRectMake(0, 0, 320, 460);
scrollView = [[UIScrollView alloc] initWithFrame:scrollViewFrame];
scrollView.delegate = self;
scrollView.minimumZoomScale = 1;
scrollView.maximumZoomScale = 4.0;
[self.view addSubview:scrollView];
CGSize scrollViewContentSize = CGSizeMake(640, 800);
[scrollView setContentSize:scrollViewContentSize];
NSMutableArray *datos = [[NSMutableArray alloc]initWithObjects:#"ob1",#"ob2",#"ob3",#"ob4", nil];
for (int i = 0; i < [datos count]; i++) {
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self action:#selector(aMethod)
forControlEvents:UIControlEventTouchUpInside];
button.frame = CGRectMake(100, 100*i, 70, 70);
button.tag = i;
[button setTitle: #"Line1" forState: UIControlStateNormal];
button.hidden= NO;
[scrollView addSubview:button];
}
}
-(void)aMethod{
}
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{
return scrollView.superview;
}
You should setup a subview container and pass that as the viewForZoomingInScrollView.
The subview should contain your buttons:
self.buttonSubview = [[UIView alloc] initWithFrame:...];
...
[buttonSubview addSubview:button];
...
[scrollView addSubview:buttonSubview];
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{
return self.buttonSubview;
}

Showing UIButton Done on UIScrollview for images

Want to show UIButton done on UIScrollView for images. With the below code UIButton is showing but when I scroll UIImageViews, it shows only on very first imageview where as I want UIButton done should show on all image views in UIScrollView.
- (void)viewDidLoad
{
self.view.backgroundColor = [UIColor blackColor];
UIScrollView *imageScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeCustom];
[myButton addTarget:self action:#selector(dismissView:) forControlEvents:UIControlEventTouchUpInside];
myButton.frame = CGRectMake(0, 0, 60, 35);
[myButton.layer setMasksToBounds:YES];
[myButton.layer setCornerRadius:10.0f];
myButton.layer.borderWidth = 2;
myButton.layer.borderColor = [[UIColor whiteColor] CGColor];
[myButton setTitle:#"Done" forState:UIControlStateNormal];
myButton.backgroundColor = [UIColor blackColor];
UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithCustomView:myButton];
[[self navigationItem] setRightBarButtonItem:button animated:YES];
imageScrollView.pagingEnabled = YES;
NSInteger numberOfViews = 61;
for (int i = 0; i < numberOfViews; i++) {
CGFloat xOrigin = i * self.view.frame.size.width;
NSString *imageName = [NSString stringWithFormat:#"image%d.png", i];
UIImage *image = [UIImage imageNamed:imageName];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
imageView.frame = CGRectMake(xOrigin, 0, self.view.frame.size.width, self.view.frame.size.height);
[imageScrollView addSubview:imageView];
[imageScrollView addSubview:myButton];
[imageView release];
}
imageScrollView.contentSize = CGSizeMake(self.view.frame.size.width * numberOfViews, self.view.frame.size.height);
[self.view addSubview:imageScrollView];
[imageScrollView release];
}
How should I code it that UIButton Done should show on all image views when I scroll it.
Add your UIButton in for loop i.e. it can add each time when your new ImageView is add.
And change its x_Origin for each imageView -
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIScrollView *imageScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
imageScrollView.pagingEnabled = YES;
NSInteger numberOfViews = 61;
for (int i = 0; i < numberOfViews; i++)
{
CGFloat xOrigin = i * self.view.frame.size.width;
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeCustom];
[myButton addTarget:self action:#selector(dismissView) forControlEvents:UIControlEventTouchUpInside];
myButton.frame = CGRectMake(xOrigin, 0, 60, 35);
[myButton setTitle:#"Done" forState:UIControlStateNormal];
myButton.backgroundColor = [UIColor blackColor];
[myButton.layer setMasksToBounds:YES];
[myButton.layer setCornerRadius:10.0f];
myButton.layer.borderWidth = 2;
myButton.layer.borderColor = [[UIColor whiteColor] CGColor];
NSString *imageName = [NSString stringWithFormat:#"image%d.png", i];
UIImage *image = [UIImage imageNamed:imageName];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
imageView.frame = CGRectMake(xOrigin, 0, self.view.frame.size.width, self.view.frame.size.height);
[imageScrollView addSubview:imageView];
[imageScrollView addSubview:myButton];
}
imageScrollView.contentSize = CGSizeMake(self.view.frame.size.width * numberOfViews, self.view.frame.size.height);
[self.view addSubview:imageScrollView];
}
Remove these 2 lines
UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithCustomView:myButton];
[[self navigationItem] setRightBarButtonItem:button animated:YES];
There is no need to do this. This means you're setting that button on rightBarButton of NavigationBar but in your case i think you dont want to do so.
Set frame for myButton in for loop similar to imageview frame.
myButton.frame= CGRectMake(xOrigin, 0, self.myButton.frame.size.width, self.myButton.frame.size.height);

Resources