UIScrollView clickable images iPhone - iphone

I have created a UIScrollView in my app - adapted from this sample code: http://developer.apple.com/iphone/library/samplecode/Scrolling/Introduction/Intro.html
But I have no idea how to make the images clickable
Any suggestions please?
Thanks!

Yeah... "Put them on UIButtons"
//get the image to use, however you want
UIImage* image = [product.images objectAtIndex:i];
UIButton* button = [[UIButton alloc] init];
//set the button states you want the image to show up for
[button setImage:image forState:UIControlStateNormal];
[button setImage:image forState:UIControlStateHighlighted];
//create the touch event target, i am calling the 'productImagePressed' method
[button addTarget:self action:#selector(productImagePressed:)
forControlEvents:UIControlEventTouchUpInside];
//i set the tag to the image #, i was looking though an array of them
button.tag = i;
//add the new button w/ the image to your scrollview
[ImagesScroller addSubview:button];
[button release];

Put them on UIButtons.

Related

Unable to set Title of UIButton

I am new to iPhone,
xpos=30;
ypos=40;
for(int i=0;i<[MyArray count];i++)
{
if(i!=0)
{
if (i%4==0)
{
ypos+=180;
xpos=30;
}
else
{
xpos+=200;
}
}
button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(xpos, ypos, 110.0, 130.0);
[button setImage: [UIImage imageNamed:#"ibook2.png"] forState:UIControlStateNormal];
[button setTitleEdgeInsets:UIEdgeInsetsMake(ypos, xpos, 0.0, 0.0)];
[button setTitle:[NSString stringWithFormat:#"%#", [MyArray objectAtIndex:i]] forState:UIControlStateNormal];
[button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
[self.view addSubview:button];
}
i am able to see button but unable to see my text on the UIButton.
Any help will be appreciated.
Please set buttons background image.You have set buttons image.
Please use this:
[button setBackgroundImage:[UIImage imageNamed:#"blue_button.png"]
forState:UIControlStateNormal];
For Wrapping button title User:
[button setTitle:#"Test button name 123" forState:UIControlStateNormal];
button.titleLabel.numberOfLines=2;
button.titleLabel.lineBreakMode = UILineBreakModeWordWrap;
You can't see the text because you have used setImage: function of button. At a time you can display image or button on text but you can add image as a background so you can see both image and tittle. In short set image as a background.
[btn setBackgroundImage:[UIImage imageNamed:#"imageName"] forState:UIControlStateNormal];
Please also remove the line of setting inset.
Rather then setting
[button setImage: [UIImage imageNamed:#"ibook2.png"] forState:UIControlStateNormal]; // This will not allow you to set Button title.
You have to use :
[button setBackgroundImage: [UIImage imageNamed:#"ibook2.png"] forState:UIControlStateNormal]; // This will allow you to set title along background button image.
if you image is too big then it will only show the image and text get truncated due to image as text will be visible after image, try using setBackground image or try small resolution image or setImageEdgeInsets .

adding custom UIButton to UINavigationBar

So the code I have is as follows:
// Create a containing view to position the button
UIView *containingView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 23.5, 21.5)] autorelease];
// Create a custom button with the image
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:[UIImage imageNamed:#"Settings.png"] forState:UIControlStateNormal];
[button addTarget:self action:#selector(settings) forControlEvents:UIControlEventTouchUpInside];
[button setFrame:CGRectMake(-19, -3, 21.5, 21.5)];
[containingView addSubview:button];
// Create a container bar button
UIBarButtonItem *containingBarButton = [[[UIBarButtonItem alloc] initWithCustomView:containingView] autorelease];
self.navigationItem.rightBarButtonItem = containingBarButton;
The issue is that my Settings.png original image size is 21.5, 21.5 and there fore the button is super hard to click. I have to tap really hard in order to trigger the UITouchUpInside to be triggered. This will clearly be rejected if I put it in the app store, as it is not in compliance with apple's HIG. Is there a way around this? I still need to use UIView as the container for the UIButton as I'd like to position it correctly on the UINavigationBar
Try to set a bigger fram to the button like 40,40 and set the content mode to be the center so the image will apear to be in the center.
button.contentMode = UIViewContentModeCenter;
Tell me if that helped

how to change button image in tableview when that button clicked

i need to place a check box in UItableview.
For that i place a box with out check image is place on button.
my code is,
UIButton *customButton = [UIButton buttonWithType:UIButtonTypeCustom];
customButton.frame=CGRectMake(100, 10, 100, 100);
[customButton setImage:[UIImage imageNamed:#"unselected.png"] forState:UIControlStateNormal];
[cell.contentView addSubview:customButton];
Now i need to change button image when button clicked.
where can i done this.
and how can i impalement this.
can any one please help me.
Thank u in advance.
//add this line
[customButton addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
//then
-(void) buttonClicked:(id) sender
{
UIButton * button = (UIButton*) sender;
[button setImage:[UIImage imageNamed:#"selected.png"] forState:UIControlStateNormal];
}
You can write this line of code
[customButton setImage:[UIImage imageNamed:#"unselected2.png"] forState:UIControlEventTouchDown];
before
[cell.contentView addSubview:customButton];
and you ca select different images for different events and states

Temporarily change a UIButtons image

I have a UIButton which I assign an image in IB.
I then change the image in the code, but I only want this to be temporary.
How does one return the button to the image set in IB or is there a way to revert back after doing the temporary switch?
Thanks
If you want to revert back to the previous image you would have to store a reference to it somewhere:
// prevImage is an ivar UIImage *
prevImage = [[myButton imageForState:UIControlStateNormal] retain];
// Change to the new image
...
// Later: revert back to prevImage
[myButton setImage:prevImage forState:UIControlStateNormal];
[prevImage release];
prevImage = nil;
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:[UIImage imageNamed:#"image1.png"] forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:#"image2.png"] forState:UIControlStateHighlighted];
[button setImage:[UIImage imageNamed:#"image3.png"] forState:UIControlStateSelected];
[button setImage:[UIImage imageNamed:#"image4.png"] forState:UIControlStateDisabled];
You can set your own custom images for different states. when you tap on it, the corresponding images will appear on your button.

how to add a ui button that calls a number from an array and displays the phone number as the button title

anotherViewController.myLabel = [NSString stringWithFormat:#"Telephone %#",
anotherViewController.title];
this is the declaration for a previus label however im not sure how to do this for a button...
the data array is located made in this format
phoneNumbers = [[NSArray alloc] initWithObjects:#"6940313388", #"4045434218", nil ];
self.phoneNumbers =phoneNumbers;
and then the labels are simply added by
theLabel.text = myLabel;
where mylabel is an NSString
please help ive been trying to solve this for a very long time ...
this is the code i use to make buttons. then you add the button to a view !
edit: you would change the setTitle: part of this to whatever string you wanted the button to display as its title.
//create the button
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
//sets its size
[button setFrame:CGRectMake(10, 10, 300, 50)];
//set title, font size and font color
[button setTitle:#"Sign Up" forState:UIControlStateNormal];
[button setFont:[button.font fontWithSize:22]];
[button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[button setBackgroundImage:[UIImage imageNamed:#"green_btn_bg.png"] forState:UIControlStateNormal];
//set action of the button
[button addTarget:self action:#selector(signUp:)
forControlEvents:UIControlEventTouchUpInside];