add label to UIButtons in iCarousel - uibutton

I implemented iCarousel method in my ViewController and all is working properly but there is one thing, i want to add below the carousel view label and for each image, i want to change the text in this label area. Here is my code I used for creating each image as button.
Thanks in advance.
UIImage *buttonImage =[NSArray arrayWithObjects:[UIImage imageNamed:#"Hotels.png"],
[UIImage imageNamed:#"image2.png"],
[UIImage imageNamed:#"image3.png"],
[UIImage imageNamed:#"image4.png"],
[UIImage imageNamed:#"image1.png"],
[UIImage imageNamed:#"FastFood.png"],
[UIImage imageNamed:#"Taxi.png"],nil];
UIButton *button =[UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0, 0, 200.0f, 200.0f);
[button setImage:[buttonImage objectAtIndex:index]forState:UIControlStateNormal];
[button addTarget:self action:#selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
return button;

iCarousel has a delegate method:
- (void)carouselCurrentItemIndexDidChange:(iCarousel *)carousel;
Use that method to update your label as it will fire every time the centred item in the carousel updates.
To actually work out which label text to use, you probably want to create a second array of strings to match your array of images. That way you can use the carousel.currentItemIndex property as the index into the strings array to select the correct label.

Related

how to fit image inside a circular button objective c

I created a button in storyboard with a circular form using layer.cornerRadius in the key path. I want to add an image to it. How can I add an image to my button and the image is fit the same size of my button
Use this to set image size to adjust button size:
[btn setImage:[UIImage imageNamed:#"image_name"] forState:UIControlStateNormal];
btn.imageView.contentMode = UIViewContentModeScaleAspectFit; //set to fit button size
It should be like this
UIImage *btnImage = [UIImage imageNamed:#"image.png"];
[buttonName setImage:btnImage forState:UIControlStateNormal];
Write the below code in viewDidAppear instead of viiewDidLoad
buttonName.layer.masksToBounds = YES;
buttonName.layer.cornerRadius = 20;//half of the button height
Try this to fit image to Button :
UIButton *button =[[UIButton alloc]initWithFrame: CGRectMake(10, 10, 50, 32)];
[button setImage:[UIImage imageNamed:#"image_name"] forState:UIControlStateNormal];
// to set image to fit button size
button.imageView.contentMode = UIViewContentModeScaleAspectFit;
//ScaleToFill (UIViewContentModeScaleToFill)
//ScaleAspectFit (UIViewContentModeScaleAspectFit)
//ScaleAspectFill (UIViewContentModeScaleAspectFill)

setting background on UIButton on Map Callout

Hi I am trying to set a background image on my detail disclosure UIButton attached to my MapView MKAnnotation callout view. Checking on stackoverflow there are consistent examples of how to set this, however it doesn't seem to work for my callout UIButton. My question is, does this not work, simply because of the mapview or am I missing something else?
Here is my code:
// If an existing pin view was not available, create one.
pinView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:#"CustomPinAnnotationView"];
pinView.canShowCallout = YES;
pinView.image = [UIImage imageNamed:#"Tch4"];
//pinView.calloutOffset = CGPointMake(0, 32);
pinView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
UIImage *btnImage = [UIImage imageNamed:#"4stars"];
[pinView.rightCalloutAccessoryView setBackgroundImage:btnImage forState:UIControlStateNormal];
I receive the error on the final line. Error is: No Visable interface for UIView declares the selector 'setbackgroundimage: forstate'
Thanks for your help with this
Your problem is rightCalloutAccessoryView returns a UIView instance. And UIView does not respond to setBackgroundImage:forState: method.
What you are looking for is to add a UIButton instead something like this:
UIButton *infoButton = [UIButton buttonWithType:UIButtonTypeCustom];
infoButton.frame = CGRectMake(0, 0, 32, 32); // Set your own frame
[infoButton setBackgroundImage:btnImage forState:UIControlStateNormal];
[infoButton setBackgroundImage:btnImage forState:UIControlStateSelected];
pinView.rightCalloutAccessoryView = infoButton;
you cant set background image if your typebutton DetailDisclosure.
you can addsubview image to rightCalloutAccessoryView but without button..
so if you still use typebutton DetailDisclosure you cant set background

Animation on button using images

I have a UIButton which has arrow kind of blue image set as
UIImage *arrowImage = [UIImage imageNamed:#"bluearrow.png"];
[self.prevbutton setBackgroundImage:arrowImage forState:UIControlStateNormal];
Now I have another image yellow.png, that I want to super impose on top of this button image and make a flashing animation continuously. How can I do this?
This should help you.
myButton.imageView.animationImages =
[NSArray arrayWithObjects:[UIImage imageNamed:#"image1.png"],
[UIImage imageNamed:#"image2.png"],
nil];
myButton.imageView.animationDuration = 0.5; //whatever you want (in seconds)
[myButton.imageView startAnimating];
As quoted from https://stackoverflow.com/a/7490535/716216

Distorted custom Navigation buttons on non-retina display (using stretchable image method)

I am adding custom navigation buttons to my navigation bars via the following code.
//Instance method in CustomNavButton Class
-(UIButton*)setupButtonWithImage:(UIImage*)image andFrame:(CGRect)frame
{
UIButton *button = [[[UIButton alloc]initWithFrame:frame]autorelease];
UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake((frame.size.width-20)/2, (frame.size.height-20)/2, 20, 20)];
imageView.image = image;
UIImage *buttonImageNormal = [UIImage imageNamed:#"customBtn_black"];
UIImage *stretchableButtonImageNormal = [buttonImageNormal
stretchableImageWithLeftCapWidth:12 topCapHeight:0];
button.titleLabel.font = [UIFont boldSystemFontOfSize:12];
[button setBackgroundImage:stretchableButtonImageNormal
forState:UIControlStateNormal];
[button setTitleShadowColor:[UIColor whiteColor] forState:UIControlStateNormal];
[button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[button addSubview:imageView];
return button;
}
//Call CustomNavButton and add to Navbar
- (void)viewDidLoad
{
[super viewDidLoad];
//Add left invite friends button
CustomNavButton *leftButton = [[CustomNavButton alloc]initWithImage:[UIImage imageNamed:#"friends_1"] andFrame:CGRectMake(0, 0, 40, 32)];
[leftButton.customNavButton addTarget:self action:#selector(inviteButtonPressed) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *leftBarButton = [[UIBarButtonItem alloc]initWithCustomView:leftButton];
self.navigationItem.leftBarButtonItem = leftBarButton;
[leftButton release];
}
The navigation buttons appear fine on my iPhone (IOS5 with Retina Display)
However, the buttons look distorted on my simulator (or non-retina display)
How can I resolve this? How can I display the buttons properly even for non-retina displays?
Note that I have created the #2x buttons for this as well
EDIT:
It seems like the issue likes with the stretching of the image
UIImage *stretchableButtonImageNormal = [buttonImageNormal
stretchableImageWithLeftCapWidth:12 topCapHeight:0];
If I change the leftCapWidth value to 0, the buttons on the simulator looks better (but still bad).
But by doing this, it will cause my retina display button look a little distorted (seems like I can't win).
Can anyone advise if the problem does really lie here, and how I can alter the values so that it works well for both retina and non-retina displays?
Do you have two files? MyImage.png and MyImage#2x.png? It looks like the system is trying to resize the #2x file by scaling it down, which usually causes some jaggedness like this when it is simply scaled down.
MyImage.png should be half the size of MyImage#2x.png.
Your not setting the file format .png or whatever it is you have. And as Chris said you need two files regular and #2x.

UIButton UIControlEventTouchUpInside does not fire, but UIControlEventTouchDown does fire

I am implementing UIButton and my UIControlEventTouchUpInside event does not fire, even though UIControlEventTouchDown does fire.
UIButton *btnClose = [UIButton buttonWithType:UIButtonTypeCustom];
NSString *filePathImage = #"img.png";
NSString *filePathImageTap = #"img-tap.png";
UIImage *buttonImage = [UIImage imageWithContentsOfFile:filePathImage];
UIImage *buttonImageTap = [UIImage imageWithContentsOfFile:filePathImageTap];
[btnClose setImage:buttonImage forState:UIControlStateNormal];
[btnClose setImage:buttonImageTap forState:UIControlStateSelected];
[btnClose setImage:buttonImageTap forState:UIControlStateHighlighted];
[btnClose addTarget:self action:#selector(close:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btnClose];
I have an assumption why it doesn't take the UIControlEventTouchUpInside:
I had two "BannerView" (Advertisement) in two different ViewControllers. In one it just worked fine in the other I saw how the UIButton was touched and the second image (darker) appeared. But the selector was not hit. Then I realized, that the Up-Event was absorbt by a UISwipeGestureRecognizer from a UIImageView below. Some Recognizer compete against each other. I searched for an overview to see witch ones these are, but found nothing.
Another solution:
If you see that the ButtonState doesn't change u have to look if you have a View over your Button.