UIScrollview not adding buttons - iphone

I have defined a uiscrollview and used a for loop to add buttons to it, but whenever I run the program, I come up with a blank screen.
Here is my viewDidLoad, this is all the code i have relating to this.
- (void)viewDidLoad
{
[super viewDidLoad];
scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, 320, 1000)];
scrollView.hidden = NO;
names = [[NSMutableArray alloc]initWithObjects:#"Excavations",#"Concrete",#"Framing",#"Insulation",#"Drywall",#"Finish Carpentry",#"Paint",#"Electrical",#"HVAC",#"Plumbing",#"Roofing",#"Tile",#"Doors",#"Windows",#"Flooring",#"Countertops",#"Landscape",#"Hardscape", nil];
for (int i = 0; i < 18; i++) {
UIButton *button = [[UIButton alloc]init];
button.frame = CGRectMake(0, 50*i, 320, 50);
button.tag = i;
[button addTarget:self action:#selector(NewView:) forControlEvents:UIControlEventTouchUpInside];
button.titleLabel.text = [names objectAtIndex:i];
button.hidden= NO;
[scrollView addSubview:button];
}
// Do any additional setup after loading the view, typically from a nib.
}

You need to set up the type for button manually, or just initialize with it:
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
Edit: you also forgot to add your scrollview to main view
[self.view addSubview:scrollView];

You have to add the UIScrollView to your view. Try
...
scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, 320, 1000)];
scrollView.hidden = NO;
[self.view addSubview:scrollView];
...

Related

Trouble Setting Button Image When On a ScrollView

I'm making an app. My app is based off this sample project. https://github.com/yeahdongcn/RSCircaPageControl
In the app there is a view. On the view, there is a scroll view (UIScrollView *scrollView) which allows to 10 "pages" of content. Within each page, there is another scroll view (UIScrollView *sv) which is a subview of *scrollView.
On SV, I then added a button called UIButton *button. When I attempt to change the button's images after a time of "two minutes," nothing happens. This is because there are 10 buttons. I need to change the image of the button on only one of the pages. Not all of them.
Something tells me it needs "object at index" or something to specify which button on which page I want to change.
The code is below! Thanks guys!
- (void)viewDidLoad
{
[super viewDidLoad];
self.clipView = [[RSView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)];
self.clipView.clipsToBounds = YES;
[self.view addSubview:self.clipView];
self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.clipView.bounds.size.width, kScrollViewHeight)];
self.scrollView.delegate = self;
self.scrollView.clipsToBounds = NO;
self.scrollView.pagingEnabled = YES;
self.scrollView.showsHorizontalScrollIndicator = NO;
self.scrollView.showsVerticalScrollIndicator = NO;
self.scrollView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
[self.view addSubview:self.scrollView];
self.clipView.scrollView = self.scrollView;
self.pageControl = [[RSCircaPageControl alloc] initWithNumberOfPages:10];
CGRect frame = self.pageControl.frame;
frame.origin.x = self.view.bounds.size.width - frame.size.width - 10;
frame.origin.y = roundf((self.view.bounds.size.height - frame.size.height) / 2.);
self.pageControl.frame = frame;
self.pageControl.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin;
[self.pageControl setCurrentPage:0 usingScroller:NO];
[self.view addSubview:self.pageControl];
CGFloat currentY = 0;
for (int i = 0; i < 10; i++) {
UIScrollView *sv = [[UIScrollView alloc] initWithFrame:CGRectMake(0, currentY, self.scrollView.bounds.size.width, kScrollViewHeight)];
sv.tag = kScrollViewTagBase + i;
sv.delegate = self;
sv.autoresizingMask = UIViewAutoresizingFlexibleWidth;
sv.backgroundColor = [UIColor colorWithRed:(arc4random() % 257) / 256. green:(arc4random() % 257) / 256. blue:(arc4random() % 257) / 256. alpha:1];
///here's the buttton!!
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage*normal = [UIImage imageNamed:#"girl.jpg"];
UIImage*selected = [UIImage imageNamed:#"girl2.jpg"];
button.frame = CGRectMake(100, 100, 50,50));
[button setImage:normal forState:UIControlStateNormal];
[button setImage:selected forState:UIControlStateHighlighted];
button.contentMode = UIViewContentModeScaleAspectFit;
[button addTarget:self
action:#selector(myButtonPressed:)
forControlEvents:UIControlEventTouchDown];
[sv addSubview:button]; //notice that the button is added as a subview of SV
[self.scrollView addSubview:sv]; // notice that SV is a subview of the scrollView.
if (i == 2 || i == 6) {
sv.contentSize = CGSizeMake(sv.contentSize.width, kScrollViewContentHeight);
}
currentY += kScrollViewHeight;
}
self.scrollView.contentSize = CGSizeMake(self.scrollView.contentSize.width, currentY);
[self performSelector:changeButtomImages withObject:nil afterDelay:TwoMinutes];
}
- (void)changeButtonImages {
UIImage*normal2 = [UIImage imageNamed:#"boy.jpg"];
UIImage*selected2 = [UIImage imageNamed:#"boy2.jpg"];
[button setImage:normal2 forState:UIControlStateNormal];
[button setImage:selected2 forState:UIControlStateHighlighted];
}
Kyle
for setting button image in a normal state the parameter to pass forState: is UIControlStateNormal not UIControlStateHighlighted
So, in changeButtonImages you are changing the the image for the iVar "button", but you are never actually assigning anything to that button var. In your for loop in viewDidLoad, you are creating a local variable of UIButton called button. Maybe something like this?
- (void)myButtonPressed:(id)sender
{
UIButton *button = (UIButton *)sender;
UIImage*normal2 = [UIImage imageNamed:#"boy.jpg"];
UIImage*selected2 = [UIImage imageNamed:#"boy2.jpg"];
[button setImage:normal2 forState:UIControlStateNormal];
[button setImage:selected2 forState:UIControlStateHighlighted];
}

adding a button in an UIView

I'm trying to add a button to a UIview, to cover the whole area of the view.. like this:
-(MBNHomeScreenButton*) initWithImage:(UIImage*)image
andHighlightedImage:(UIImage*)highlightedImage
andTitle:(NSString*)aTitle
withSelector:(SEL)actionButton
forDelegate:(UIViewController*)viewController{
self = [super init];
if (self) {
self.frame = CGRectMake(0, 0, 100, 120);
self.imageView = [[UIImageView alloc] initWithImage:image highlightedImage:highlightedImage];
self.imageView.center = self.center;
self.imageView.frame = CGRectMake(10, 0, HS_BUTTON_IMAGE_WIDTH, HS_BUTTON_IMAGE_HEIGHT);
self.imageView.contentMode = UIViewContentModeScaleAspectFit;
self.imageView.highlighted = NO;
[self addSubview:self.imageView];
self.title = [[UILabel alloc] initWithFrame:CGRectMake(0, HS_BUTTON_IMAGE_HEIGHT, HS_BUTTON_IMAGE_WIDTH + 20, 30)];
self.title.text = aTitle;
self.title.textAlignment = UITextAlignmentCenter;
self.title.textColor = [UIColor whiteColor];
self.title.backgroundColor = [UIColor clearColor];
[self addSubview:self.title];
// This creates a transparent button over the view
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
//button.backgroundColor = [UIColor redColor];
[button setFrame:self.frame];
[button addTarget:viewController action:actionButton forControlEvents:UIControlEventTouchUpInside];
[self addSubview: button];
}
return self;}
In the ViewController I create the View like this:
m_newsHSButton = [[MBNHomeScreenButton alloc] initWithImage:[UIImage imageNamed:NEWS_SHADED_IMAGE]
andHighlightedImage:[UIImage imageNamed:NEWS_HIGHLIGHTED_IMAGE]
andTitle:#"News"
withSelector:#selector(newsButtonPressed)
forDelegate:self];
m_newsHSButton.center = CGPointMake(m_backgroundView.frame.size.width / 4, m_backgroundView.frame.size.height / 4);
[backgroundImgView addSubview:m_newsHSButton];
Still... the newsButtonPressed doesn't get called.
Can you help?
Thanks!
====================
[edit]
Ok... figured it out.. Thanks guys.
It was the fact that I was trying to add the view as a subview to a UIImageView with the last line:
[backgroundImgView addSubview:m_newsHSButton];
Apparently, it won't get touches anymore.
Thanks for the effort guys!
Try making the button a #property or instance variable in your custom view. Then call [m_newsHSButton.button addTarget:self action:actionButton forControlEvents:UIControlEventTouchUpInside]; from the viewController.

Adding a view to UIScrollview

I have created a UIView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
UIImage *image2 = [UIImage imageNamed:#"pinGreen_v1.png"];
UIImage *image1 = [UIImage imageNamed:#"PinDown1.png"];
UIButton *btn1 = [[UIButton alloc]initWithFrame:CGRectMake(0, 10, 40, 40)];
[btn1 setImage:image2 forState:UIControlStateNormal];
[self addSubview:btn1];
for(int i =1; i<20; i++){
UIButton *btn2 = [[UIButton alloc]initWithFrame:CGRectMake(40 * i, 40, 40, 40)];
[btn2 setImage:image1 forState:UIControlStateNormal];
[btn2 addTarget:self
action:#selector(buttonPressed)
forControlEvents:UIControlEventTouchUpInside];
[self addSubview:btn2];
}
}
return self;
}
With the above code I am drawing images in my view. I am loading this view from my view controller. I need to put this view inside a UIScrollview. Where should I create the scroll view? In the above view or the view controller where the above view is created.
The code inside my view controller is :
DrawLineInMyClass *drawLines = [[DrawLineInMyClass alloc]initWithFrame:CGRectMake(0, 0, 320, 400)];
drawLines.backgroundColor = [UIColor blackColor];
[self.view addSubview:drawLines];
[drawLines release];
I should be able to scroll through the images inside my view.
I would create the the scrollview in the viewcontroller and add the view to it.
UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
scroll.pagingEnabled = YES;
DrawLineInMyClass *drawLines = [[DrawLineInMyClass alloc]initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
drawLines.backgroundColor = [UIColor blackColor];
[self.view addSubview:drawLines];
[scroll addSubview:drawLines];
scroll.contentSize = CGSizeMake(self.view.frame.size.width*drawLines.frame.size.width, self.view.frame.size.height);
[self.view addSubview:scroll];
[drawLines release];
[scroll release];

how add image with scroll view instead of uibutton in scrolling view?

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];

IPhone picture gallery source code

HI all
can anyone please refer me, the iphone image gallery source code?
Any link ,sample code will be great help.
i am trying to show some 70 to 100 images as thumbnails , and on selecting any image, it should give a full view of that image,i am trying to accomplish, whatever is in iphone's picture gallery,
i thought, there must any sample code available.
suggestions are always appreciated
regards
In didload method call two methods to create scroll view and thumbnail button.Keep both thumbnail images and wallpaper images array in the same order.
-(void)createScrollView
{
scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 440)];
scrollView.pagingEnabled = YES;
scrollView.showsHorizontalScrollIndicator = NO;
scrollView.showsVerticalScrollIndicator = NO;
scrollView.scrollsToTop = NO;
scrollView.delegate = self;
scrollView.contentSize = CGSizeMake(320 * (([imagesArray count]- 1) / 25 + 1), 440);
scrollView.backgroundColor=[UIColor blackColor];
[self.view addSubview:scrollView];
}
-(void)createButton{
for (int i = 0; i < [imagesArray count]; i++)
{
thumbNailButton = [UIButton buttonWithType:UIButtonTypeCustom];
thumbNailButton.frame = CGRectMake(6 + 62 * (i % 5) + 320 * (i / 25), 5+65 * ((i / 5) % 5), 56,56);
img = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 56, 56)];
[img setImage:[UIImage imageNamed:[imagesArray objectAtIndex:i]]];
thumbNailButton.tag=i;
[thumbNailButton addTarget:self action:#selector(imageClicked:) forControlEvents:UIControlEventTouchUpInside];
[thumbNailButton addSubview:img];
[scrollView addSubview:thumbNailButton];
}
}
-(void)imageClicked:(id)sender{
UIButton* button = (UIButton*)sender;
AppDelegate *appDelegate=(AppDelegate *)[[UIApplication sharedApplication]delegate];
[appDelegate setimageClickedValue:button.tag];
LargeWallPaperviewController *largeWallPaperViewController=[[LargeWallPaperviewController alloc]initWithNibName:#"LargeWallPaperviewController" bundle:nil];
[self.navigationController pushViewController:largeWallPaperViewController animated:YES];
[largeWallPaperViewController release];
}
In largewallpaperviewcontroller class in didload method
[imagesArray addObjectsFromArray:[NSArray arrayWithObjects:#"wallf1.jpg",#"wallf2.jpg",#"wallf3.jpg",#"wallf4.jpg",#"wallf5.jpg",#"wallf6.jpg",#"wallf7.jpg",nil]];
scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 440)];
scrollView.pagingEnabled = YES;
scrollView.showsHorizontalScrollIndicator = NO;
scrollView.showsVerticalScrollIndicator = NO;
scrollView.scrollsToTop = NO;
scrollView.delegate = self;
scrollView.contentSize = CGSizeMake(320 * ([imagesArray count] ), 440);
scrollView.backgroundColor = [UIColor blackColor];
[self.view addSubview:scrollView];
for (int i = 0; i < [imagesArray count]; i++)
{
wallPaperButton=[UIButton buttonWithType:UIButtonTypeCustom];
wallPaperButton.tag=i;
wallPaperButton.frame=CGRectMake((320*i),0, 320, 325);
UIImageView *img =[ [UIImageView alloc]initWithFrame:CGRectMake(0,0, 320, 325)];
img.image=[UIImage imageNamed:[imagesArray objectAtIndex:i]];
img.contentMode=UIViewContentModeScaleAspectFit;
[wallPaperButton addSubview:img];
[img release];
[wallPaperButton addTarget:self action:#selector(imageSelected:) forControlEvents:UIControlEventTouchUpInside]; [scrollView addSubview:wallPaperButton];
}
appDelegate=(AppDelegate *)[[UIApplication sharedApplication]delegate];
int imageValue=[appDelegate getimageClickedValue];
[scrollView scrollRectToVisible:CGRectMake(320*imageValue, 0, 320 , 440) animated:NO];
i have used a button in the largewallpaer view.If you want you remove it and directly add it to image view.This code is working for me ,change it to your requirement.Its easy to understand.
All the best.
Hats off to this code. This works perfectly. Although I have modified the "appdelegate part" since I was using story boards.
Here is what I did
-(void)imageClicked:(id)sender{
UIButton* button = (UIButton*)sender;
imageNumber = button.tag;
[self performSegueWithIdentifier:#"loadImage" sender:sender];
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:#"loadImage"])
{
[segue.destinationViewController setimageValue:imageNumber];
}
}
And in my destination View Controller.m file named 'ImageViewController' I implemented things as below
-(void)setimageValue:(int)number;
{
self.imageValue = number;
}
And in viewDidLoad, instead of app delegate I just used
[self.scrollView scrollRectToVisible:CGRectMake(320*self.imageValue, 0, 320 , 440) animated:NO];