How can I add a custom divider image to the UITabBar? - iphone

I have a UITabBar that I am trying to style after the new iOS 6 App Store look. I have some pretty good looking gradients, but I am wondering how to set the divider image.
Aside from manually adding the line to each image, is there anything I can do to reproduce the divider look?

You can add an image for each item , for the selected and unselected states.
Like this:
UIImage *selectedImage0 = [UIImage imageNamed:#"image1.png"];
UIImage *unselectedImage0 = [UIImage imageNamed:#"image1_unselected.png"];
UIImage *selectedImage1 = [UIImage imageNamed:#"image2.png"];
UIImage *unselectedImage1 = [UIImage imageNamed:#"image2_unselected.png"];
UIImage *selectedImage2 = [UIImage imageNamed:#"image3.png"];
UIImage *unselectedImage2 = [UIImage imageNamed:#"image3_unselected.png"];
UIImage *selectedImage3 = [UIImage imageNamed:#"image4.png"];
UIImage *unselectedImage3 = [UIImage imageNamed:#"image4_unselected.png"];
UIImage *selectedImage4 = [UIImage imageNamed:#"image5.png"];
UIImage *unselectedImage4 = [UIImage imageNamed:#"image5_unselected.png"];
UITabBar *tabBar = self.tabBarController.tabBar;
UITabBarItem *item0 = [tabBar.items objectAtIndex:0];
UITabBarItem *item1 = [tabBar.items objectAtIndex:1];
UITabBarItem *item2 = [tabBar.items objectAtIndex:2];
UITabBarItem *item3 = [tabBar.items objectAtIndex:3];
UITabBarItem *item4 = [tabBar.items objectAtIndex:4];
[item0 setFinishedSelectedImage:selectedImage0 withFinishedUnselectedImage:unselectedImage0];
[item1 setFinishedSelectedImage:selectedImage1 withFinishedUnselectedImage:unselectedImage1];
[item2 setFinishedSelectedImage:selectedImage2 withFinishedUnselectedImage:unselectedImage2];
[item3 setFinishedSelectedImage:selectedImage3 withFinishedUnselectedImage:unselectedImage3];
[item4 setFinishedSelectedImage:selectedImage4 withFinishedUnselectedImage:unselectedImage4];
You can place this code in the viewDidLoad method of any of your controllers.
Hope this helps.
Cheers!

i put my tab bar image like :-
and i set it on tap bar like:-
CGRect frame = CGRectMake(0.0, 0.0, 320, 48);
NSLog(#"%f",frame.size.width);
UIView *v = [[UIView alloc] initWithFrame:frame];
// [v setBackgroundColor:DARK_BACKGROUNDTabBar];
UIImageView *imgView =[[UIImageView alloc]initWithFrame:frame];
UIImage *img1 = [UIImage imageNamed:#"tabbar.png"];
[imgView setImage:img1];
[v addSubview:imgView];
[v setAlpha:1];
[[self.tabBarController tabBar] insertSubview:v atIndex:1];
[v release];
hope its use-full for you
you can create your own image with 4 or 5 tab and set like above

If all you want is to give the tabs a "selected" and a "normal" background image (which include one half of the divider left and right) you could subclass UITabBarController and during viewDidLoad replace the UITabBar with UIButtons. It's pretty straight forward. The images are set as the background of the buttons. They contain 4px of the divider on the left and the right side.
I am using this with a storyboard so the self.tabBar.items array is already populated when viewDidLoad is called.
#interface MyTabBarController()
#property (nonatomic,strong) UIImage *selectedImage;
#property (nonatomic,strong) UIImage *normalImage;
#end
#implementation MyTabBarController
#synthesize selectedImage, normalImage;
- (void) viewDidLoad
{
[super viewDidLoad];
// set whichever view you want to start with
self.selectedIndex = 1;
self.tabBar.hidden=YES;
// those are the images used for the selected and not selected tabs
self.selectedImage = [[UIImage imageNamed:#"tabBarBackground_down.png"] stretchableImageWithLeftCapWidth:4 topCapHeight:0];
self.normalImage = [[UIImage imageNamed:#"tabBarBackground.png"] stretchableImageWithLeftCapWidth:4 topCapHeight:0];
CGFloat width = self.view.frame.size.width/[self.tabBar.items count];
for( int itemIndex=0; itemIndex<[self.tabBar.items count]; itemIndex++ )
{
UITabBarItem *item = [self.tabBar.items objectAtIndex:itemIndex];
// create a button
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
// configure the button to act as a tab
button.adjustsImageWhenHighlighted = NO;
[button setImage:item.image forState:UIControlStateNormal];
[button setBackgroundImage:self.selectedIndex==itemIndex?self.selectedImage:self.normalImage forState:UIControlStateNormal];
// this is used later to set the selectedIndex of the UITabBarController
button.tag = itemIndex;
// position the button in place of tab
CGFloat nextXStart = itemIndex+1==[self.tabBar.items count]?self.view.frame.size.width:rint(width*(itemIndex+1));
button.frame = CGRectMake(rint(width*itemIndex), self.view.frame.size.height-49, nextXStart-rint(width*itemIndex) , 49);
[self.view addSubview:button];
// add event to the button
[button addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchDown];
}
}
- (void) buttonClicked:(id)sender
{
UIButton *button = sender;
if( self.selectedIndex!=button.tag )
{
// select the tab and change the visible viewcontroller
self.selectedIndex = button.tag;
// reset all but the active buttons to have the normal image
for( UIView *subview in self.view.subviews )
{
if( subview!=sender && [subview isKindOfClass:[UIButton class]])
[((UIButton *)subview) setBackgroundImage:self.normalImage forState:UIControlStateNormal];
}
// set the selectedImage as background of the clicked button
[button setBackgroundImage:self.selectedImage forState:UIControlStateNormal];
}
}
#end

Related

Back Button can't be seen on navigation controller in IOS6

I have an customised UINavigationcontroller created by me programatically.
The Problem is with the back button which comes as a default in UINavigationBar is not seen in IOS6 but when i press it the action can be done.
NOTE: The back button is seen in IOS5.
Here is my code that i had used
- (void)customizeNavigationController:(UINavigationController *)navController
{
UINavigationBar *navBar = [navController navigationBar];
[navBar setTintColor:keyNavBarTintColor];
UIImageView *myImageView = (UIImageView *)[navBar viewWithTag:keyNavBarBackgroundImageTag];
if (myImageView == nil)
{
UIImage *img = [UIImage imageNamed:#"image.png"];
CGRect rect = CGRectMake(0, 0, navBar.frame.size.width, navBar.frame.size.height);
myImageView = [[UIImageView alloc] initWithFrame:rect];
[myImageView setContentMode:UIViewContentModeScaleAspectFill];
[myImageView setAutoresizingMask:UIViewAutoresizingFlexibleWidth];
myImageView.image = img;
[myImageView setTag:keyNavBarBackgroundImageTag];
[navBar addSubview:myImageView];
[myImageView release];
}
self.navImageView = myImageView;
}
Try this:
UIImage *image = [UIImage imageNamed:kButtonBackInActive];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:image forState:UIControlStateNormal];
[button addTarget:self action:#selector(goBack) forControlEvents:UIControlEventTouchUpInside];
[button setFrame:CGRectMake(0, 0, 39.0f, 44.0f)];
button.contentEdgeInsets = (UIEdgeInsets){.left=-8};
button.showsTouchWhenHighlighted = NO;
UIBarButtonItem * backbutton = [[[UIBarButtonItem alloc] initWithCustomView:button] autorelease];
[self.navigationItem setLeftBarButtonItem:backbutton];

Customization of UINavigationBar and the back button

I followed the tutorial below to customize the UINavigationBar.
http://foobarpig.com/iphone/uinavigationbar-with-solid-color-or-image-background.html
I applied a background image in the UINavigationBar, however, I do not know how to customize the back button. At the moment, the default back button does not suits the look & feel of the customized UINavigationBar.
Please teach me how to change the background color or image of the default back button. Thank you.
I've written the following categories to customize the back button:
UIBarButtonItem+StyledButton.h
#interface UIBarButtonItem (StyledButton)
+ (UIBarButtonItem *)styledBackBarButtonItemWithTarget:(id)target selector:(SEL)selector;
+ (UIBarButtonItem *)styledCancelBarButtonItemWithTarget:(id)target selector:(SEL)selector;
+ (UIBarButtonItem *)styledSubmitBarButtonItemWithTitle:(NSString *)title target:(id)target selector:(SEL)selector;
#end
UIBarButtonItem+StyledButton.m
#implementation UIBarButtonItem (StyledButton)
+ (UIBarButtonItem *)styledBackBarButtonItemWithTarget:(id)target selector:(SEL)selector;
{
UIImage *image = [UIImage imageNamed:#"button_back"];
image = [image stretchableImageWithLeftCapWidth:20.0f topCapHeight:20.0f];
NSString *title = NSLocalizedString(#"Back", nil);
UIFont *font = [UIFont boldSystemFontOfSize:12.0f];
UIButton *button = [UIButton styledButtonWithBackgroundImage:image font:font title:title target:target selector:selector];
button.titleLabel.textColor = [UIColor blackColor];
CGSize textSize = [title sizeWithFont:font];
CGFloat margin = (button.frame.size.height - textSize.height) / 2;
CGFloat marginRight = 7.0f;
CGFloat marginLeft = button.frame.size.width - textSize.width - marginRight;
[button setTitleEdgeInsets:UIEdgeInsetsMake(margin, marginLeft, margin, marginRight)];
[button setTitleColor:[UIColor colorWithRed:53.0f/255.0f green:77.0f/255.0f blue:99.0f/255.0f alpha:1.0f] forState:UIControlStateNormal];
return [[[UIBarButtonItem alloc] initWithCustomView:button] autorelease];
}
+ (UIBarButtonItem *)styledCancelBarButtonItemWithTarget:(id)target selector:(SEL)selector;
{
UIImage *image = [UIImage imageNamed:#"button_square"];
image = [image stretchableImageWithLeftCapWidth:20.0f topCapHeight:20.0f];
NSString *title = NSLocalizedString(#"Cancel", nil);
UIFont *font = [UIFont boldSystemFontOfSize:12.0f];
UIButton *button = [UIButton styledButtonWithBackgroundImage:image font:font title:title target:target selector:selector];
button.titleLabel.textColor = [UIColor blackColor];
[button setTitleColor:[UIColor colorWithRed:53.0f/255.0f green:77.0f/255.0f blue:99.0f/255.0f alpha:1.0f] forState:UIControlStateNormal];
return [[[UIBarButtonItem alloc] initWithCustomView:button] autorelease];
}
+ (UIBarButtonItem *)styledSubmitBarButtonItemWithTitle:(NSString *)title target:(id)target selector:(SEL)selector;
{
UIImage *image = [UIImage imageNamed:#"button_submit"];
image = [image stretchableImageWithLeftCapWidth:20.0f topCapHeight:20.0f];
UIFont *font = [UIFont boldSystemFontOfSize:12.0f];
UIButton *button = [UIButton styledButtonWithBackgroundImage:image font:font title:title target:target selector:selector];
button.titleLabel.textColor = [UIColor whiteColor];
[button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
return [[[UIBarButtonItem alloc] initWithCustomView:button] autorelease];
}
UIButton+StyledButton.h
#interface UIButton (UIButton_StyledButton)
+ (UIButton *)styledButtonWithBackgroundImage:(UIImage *)image font:(UIFont *)font title:(NSString *)title target:(id)target selector:(SEL)selector;
#end
UIButton+StyledButton.m
#implementation UIButton (UIButton_StyledButton)
+ (UIButton *)styledButtonWithBackgroundImage:(UIImage *)image font:(UIFont *)font title:(NSString *)title target:(id)target selector:(SEL)selector
{
CGSize textSize = [title sizeWithFont:font];
CGSize buttonSize = CGSizeMake(textSize.width + 20.0f, image.size.width);
UIButton *button = [[[UIButton alloc] initWithFrame:CGRectMake(0.0f, 0.0f, buttonSize.width, buttonSize.height)] autorelease];
[button addTarget:target action:selector forControlEvents:UIControlEventTouchUpInside];
[button setBackgroundImage:image forState:UIControlStateNormal];
[button setTitle:title forState:UIControlStateNormal];
[button.titleLabel setFont:font];
return button;
}
#end
It's easy to use, e.g.:
- (void)viewDidLoad
{
[super viewDidLoad];
self.navigationItem.leftBarButtonItem = [UIBarButtonItem styledBackBarButtonItemWithTarget:self selector:#selector(dismissModalViewController)];
self.navigationItem.rightBarButtonItem = [UIBarButtonItem styledSubmitBarButtonItemWithTitle:NSLocalizedString(#"Done", nil) target:self selector:#selector(doneButtonTouched:)];
}
The above code is from a project that's still work-in-progress, so it could be cleaned up a bit, but it works as supposed to. Use images without text as buttons and make sure they're stretchable (i.e. don't make the images too small and be careful with gradients). The image of the back button in the following example is only 31 x 30 pixels, but it's stretched to make the text fit.
Some examples of the results:
Back button
Cancel / Done buttons
I have used this code a few times:
- (void)viewDidLoad {
[super viewDidLoad];
// Set the custom back button
UIImage *buttonImage = [UIImage imageNamed:#"back_button.png"];
//create the button and assign the image
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:buttonImage forState:UIControlStateNormal];
//set the frame of the button to the size of the image (see note below)
button.frame = CGRectMake(0, 0, buttonImage.size.width, buttonImage.size.height);
[button addTarget:self action:#selector(back) forControlEvents:UIControlEventTouchUpInside];
//create a UIBarButtonItem with the button as a custom view
UIBarButtonItem *customBarItem = [[UIBarButtonItem alloc] initWithCustomView:button];
self.navigationItem.leftBarButtonItem = customBarItem;
// Cleanup
[customBarItem release];
}
-(void)back {
// Tell the controller to go back
[self.navigationController popViewControllerAnimated:YES];
}
From this website.
Hope it helps!
You have to build a custom UIButton and pass it to UIBarButton initWithCustomView.
Here is what you do.
1) Add custom buttons to your navigation item in the Interface Builder:
2) In the code do the following:
#define IS_IOS7 ([[[UIDevice currentDevice] systemVersion] compare:#"7.0" options:NSNumericSearch] != NSOrderedAscending)
- (void)viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
if (IS_IOS7) {
[self shiftView:self.navigationItem.leftBarButtonItem.customView horizontallyBy:-11];
[self shiftView:self.navigationItem.rightBarButtonItem.customView horizontallyBy:11];
}
}
- (void)shiftView:(UIView*)view horizontallyBy:(int)offset {
CGRect frame = view.frame;
frame.origin.y += offset;
view.frame = frame;
}

How add a UIButton on every image of image view which is in scroll view?

i have an iPhone app in which i have several images. These images add in image view. That image view add sub view with scroll view. Now i want to add a transparent button on every image. How can i do that? I have shown my code below:
- (void)layoutScrollImages{
UIImageView *view = nil;
NSArray *subviews = [scrollView1 subviews];
// reposition all image subviews in a horizontal serial fashion
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 += (kScrollWidth);
}
}
// set the content size so it can be scrollable
[scrollView1 setContentSize:CGSizeMake((kNoImages * 500), 700)];
}
- (void)viewDidLoad{
self.view.backgroundColor = [UIColor viewFlipsideBackgroundColor];
// 1. setup the scrollview for multiple images and add it to the view controller
//
// note: the following can be done in Interface Builder, but we show this in code for clarity
[scrollView1 setBackgroundColor:[UIColor blackColor]];
[scrollView1 setCanCancelContentTouches:NO];
scrollView1.indicatorStyle = UIScrollViewIndicatorStyleWhite;
scrollView1.clipsToBounds = YES; // default is NO, we want to restrict drawing within our scrollview
scrollView1.scrollEnabled = YES;
//imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"image0.jpg"]];
[scrollView1 addSubview:imageView];
[scrollView1 setContentSize:CGSizeMake(500,700)];
scrollView1.minimumZoomScale = 1;
scrollView1.maximumZoomScale = 3;
scrollView1.delegate = self;
[scrollView1 setScrollEnabled:YES];
// pagingEnabled property default is NO, if set the scroller will stop or snap at each photo
// if you want free-flowing scroll, don't set this property.
scrollView1.pagingEnabled = YES;
// load all the images from our bundle and add them to the scroll view
NSUInteger i;
for (i = 1; i <= kNoImages; i++)
{
NSString *imageName = [NSString stringWithFormat:#"page-%d.jpg", i];
UIImage *image = [UIImage imageNamed:imageName];
UIImageView *ImageView = [[UIImageView alloc] initWithImage:image];
// setup each frame to a default height and width, it will be properly placed when we call "updateScrollList"
CGRect rect = ImageView.frame;
rect.size.height = kScrollHeight;
rect.size.width = kScrollWidth;
ImageView.frame = rect;
ImageView.tag = i; // tag our images for later use when we place them in serial fashion
UIButton *btnView2= [UIButton buttonWithType:UIButtonTypeCustom];
[btnView2 setTitle:#"view" forState:UIControlStateNormal];
[btnView2 addTarget:self action:#selector(View:)forControlEvents:UIControlEventTouchDown];
[btnView2 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
btnView2.frame=CGRectMake(0,0,460,460 );
[scrollView1 addSubview:btnView2];
scrollView1.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
[scrollView1 addSubview:ImageView];
[ImageView release];
}
[self layoutScrollImages]; // now place the photos in serial layout within the scrollview
}
-(IBAction)View:(id)sender{
NSURL *imgUrl=[[NSURL alloc] initWithString:#"http://farm4.static.flickr.com/3567/3523321514_371d9ac42f.jpg"];
NSData *imgData = [NSData dataWithContentsOfURL:imgUrl];
UIImage *img = [UIImage imageWithData:imgData];
UIImageView *imgView = [[UIImageView alloc] initWithImage:img];
[self.view addSubview:imgView];
//[self.navigationController pushViewController:ivc animated:YES];
[imgUrl release];
}
Instead of adding Imageview and transparent button on top of Imageview. You can just add UIButton customtype and set button image to your image in your scrollview. There is no need to take imageview and UIButton both. Just take UIButton and setImage and you will be fine. I tried to give you sample code below from my app. Please modify it as per your need. I think it is what you need.
First of Take all constant in one of global file. I took it in en.lproj. like below.
"TotalThumbnailCount"="6";
"Coloumn"="2";
"ThumbnailHeight"="151";
"ThumbnailWidth"="151";
//Marging between two images
"MarginX" = "6";
"MarginY" = "6";
Then initialize all your local varaibles from global variables. like below
-(void)PopulateVariables{
TotalThumbnail = [NSLocalizedString(#"TotalThumbnailCount",nil) intValue];
Colomn = [NSLocalizedString(#"Coloumn",nil) intValue];
ThumbnailHeight = [NSLocalizedString(#"ThumbnailHeight",nil) intValue];
ThumbnailWidth = [NSLocalizedString(#"ThumbnailWidth",nil) intValue];
MarginX = [NSLocalizedString(#"MarginX",nil) intValue];
MarginY= [NSLocalizedString(#"MarginY",nil) intValue];
}
Now, you should initiate your thumbnail images using UIButton from below function.
-(void)PopulateThumbnails
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
XCordinate=MarginX;
YCordinate = MarginY;file:
for(int i=1; i <=TotalThumbnail;i++){
UIButton *btnMenu = [UIButton buttonWithType:UIButtonTypeCustom];
//NSData *data =UIImageJPEGRepresentation(, 1);
[btnMenu setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:#"%d.png",i]] forState:UIControlStateNormal];
CGRect frame = btnMenu.frame;
frame.size.width=ThumbnailWidth;
frame.size.height=ThumbnailHeight;
frame.origin.x=XCordinate;
frame.origin.y=YCordinate;
btnMenu.frame=frame;
btnMenu.tag=i;
btnMenu.alpha = 1;
[btnMenu addTarget:self action:#selector(btnSelected:) forControlEvents:UIControlEventTouchUpInside];
[scrollView addSubview:btnMenu];
XCordinate = btnMenu.frame.origin.x + btnMenu.frame.size.width + MarginX;
if(i%Colomn==0)
{
XPosition = XCordinate;
YCordinate = btnMenu.frame.origin.y + btnMenu.frame.size.height + MarginY;
XCordinate = MarginX;
}
}
[pool release];
}
And then set your scrollview contentsize
scrollView.contentSize = CGSizeMake(XPosition, YCordinate);
When some one tap on any image it will goes in below event.
-(IBAction)btnSelected:(id)sender{
UIButton *btnSelected = sender;
switch (btnSelected.tag) {
}
Let me know if I miss anything and if you don't understand.. Hope this help.
You can add UITapGestureRecognizer to each image:
UITapGestureRecognizer *tapGesture = [[[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapAction:)] autorelease];
[imageView addGestureRecognizer:tapGesture];
[self.view addSubview:imageView];
When you create your imageview, create a uibutton and add it as a subview to the imageview.
Set the properties of the button to be custom type. Also set the frame of the button same as that of the imageview.
button = [UIButton buttonWithType:UIButtonTypeCustom];
Google how to create a uibutton programatically. You can then add the necessary selectors to handle button press events.
Alternatively you can create a element in Interfacebuilder. A custom class which has a uiimageview and clear uibutton. You can then use this element to add to your uiscrollview.

UIButton over UIImageView (over UIScrollView)

I want to show an image (larger then the screen of the iphone), that the user can scroll.
Isn't difficoult, i've done with this code:
in my .h file
#interface mappa1 : UIViewController <UIScrollViewDelegate> {
IBOutlet UIImageView *img;
IBOutlet UIScrollView *scroller;
}
in my .m file
UIImage *image = [UIImage imageNamed:#"prova.jpg"];
img = [[UIImageView alloc] initWithImage:image];
scroller.delegate = self;
scroller.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
scroller.contentSize = img.frame.size;
scroller.scrollEnabled = YES;
scroller.directionalLockEnabled = NO;
scroller.userInteractionEnabled = YES;
CGSize ivsize = img.frame.size;
CGSize ssize = scroller.frame.size;
float scalex = ssize.width / ivsize.width;
float scaley = ssize.height / ivsize.height;
scroller.maximumZoomScale = 1.0;
scroller.minimumZoomScale = fmin(1.0, fmax(scalex, scaley));
scroller.zoomScale = fmin(1.0, fmax(scalex, scaley));
[scroller addSubview:img];
and
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
return img;
}
and all works fine!
Now i want to add an UIButton on the UIImage, not on the UIView, because i want that if the user zooms or moves the image, the uibutton follow the movement/zoom of the uiimage.
So i add this code:
UIButton *btn = [[UIButton buttonWithType:UIButtonTypeRoundedRect] retain];
btn.frame = CGRectMake(0, 0, 100, 25);
[btn setTitle:#"Play" forState:UIControlStateNormal];
[btn addTarget:self action:#selector(buttonClick) forControlEvents:UIControlEventTouchUpInside];
[img addSubview:btn];
but the UIButton is "not-clickable"!
Of course, the UIView and the UIImageView has UserInteractionEnabled set to YES!
Thanks!
EDIT: if i write
[scroller addSubView:btn];
it works but, of course, if the user zoom in/out, the UIButton is always big 100x25.
SOLVED!
I don't know why, but if i checked UserInteractionEnabled (on the UIImageView) in Interface Builder, it doesn't works.
If i write
img.UserInteractionEnabled = YES;
it works!
i tried this and it works for me.......
UIImageView * imageView = [[UIImageView alloc]init];
[imageView setImage:[UIImage imageNamed:#"image.jpeg"]];
[imageView setFrame:CGRectMake(10,10,300,300)];
[imageView setContentMode:UIViewContentModeScaleToFill];
[imageView setUserInteractionEnabled:TRUE];
UIButton * ButtonOnImageView = [[UIButton alloc]init];
[ButtonOnImageView setFrame:CGRectMake(135,120,60,30)];
[ButtonOnImageView setImage:[UIImage imageNamed:#"image.jpeg"] forState:UIControlStateHighlighted];
[ButtonOnImageView setImage:[UIImage imageNamed:#"image2.jpeg"] forState:UIControlStateNormal];
[ ButtonOnImageView addTarget:self action:#selector(OnClickOfTheButton) forControlEvents:UIControlEventTouchUpInside];
[imageView addSubview:ButtonOnImageView];
[self.view addSubview:imageView];
This Code May be usefull to You
http://developer.apple.com/library/ios/#samplecode/Scrolling/Listings/MyViewController_m.html#//apple_ref/doc/uid/DTS40008023-MyViewController_m-DontLinkElementID_6

Cannot trigger IBAction of a UIButton from UIView

I'm new to Objective C. I created the method to construct & display a button inside a UIView (UIView is inside another View named contentView, and contentView to be added as a subview in a UIScrollView. Well the problem is that I cannot click on the generated button to trigger the IB action named playAction. Can someone pls help me? Thanks
- (void) displayCategoryBestSellers
{
//Structure: ScrollView -> ContentView -> MainView -> ImageView and Button
UIView * mainView = [[UIView alloc] initWithFrame:CGRectMake(0,0,160,105)];
mainView.userInteractionEnabled = YES;
UIImage * backgroundImage = [UIImage imageNamed:#"blackwhitesquare.png"];
UIImageView * uiImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,145,105)];
uiImageView.image = backgroundImage;
uiImageView.center = CGPointMake(mainView.frame.size.width /2, mainView.frame.size.height/2);
uiImageView.userInteractionEnabled = YES;
[mainView addSubview:uiImageView];
UIButton *playButton = [[UIButton buttonWithType:UIButtonTypeRoundedRect] retain];
playButton.frame = CGRectMake(0,0,100,90);
NSData *mydata = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:#"http://media.theseus.cataloguesolutions.com/images/72by72/324178611_0004_PG_1.jpg"]];
UIImage *myimage = [[UIImage alloc] initWithData:mydata];
[playButton setBackgroundImage:myimage forState:UIControlStateNormal];
playButton.center = CGPointMake(mainView.frame.size.width /2, mainView.frame.size.height/2);
[playButton addTarget:self action:#selector(playAction:) forControlEvents:UIControlEventTouchDown];
playButton.userInteractionEnabled = YES;
[mainView addSubview:playButton];
[contentView addSubview:mainView];
contentView.userInteractionEnabled = YES;
[scrollView addSubview:contentView];
scrollView.delegate = self;
scrollView.userInteractionEnabled = YES;
}
-(IBAction)playAction: (id)sender
{
NSLog(#"Button Clicked");
}
Use UIControlEventTouchUpInside instead of UIControlEventTouchDown.