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.
Related
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];
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
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.
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
is there any way to hide an UIButton until the UIImageView is pressed??
When the picture is pressed I need to show the back Button, like it works at the Photo App on the iPhone???
Here is the code of my UIButton:
- (void)viewDidLoad {
[super viewDidLoad];
[self ladeImage];
UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn.frame = CGRectMake(10, 10, 40, 40);
[btn addTarget:self action:#selector(goToViewA) forControlEvents:UIControlEventTouchUpInside];
[btn setTitle:#"<<" forState:UIControlStateNormal];
[self.view addSubview:btn];
}
First step : btn.hidden = YES
Then you have to subclass the UIImageView to react to its touchesEnded: event and change the hidden property of your button there. For that, the proper way is to create a protocol (with a viewTouched method). Implement that protocol in the viewController containing your button and you ImageView. Add a delegate propery to the subclassed ImageView (i.e. id<MyCustomProtocol> _delagate;) and assign the view controller to this propery.
btn.hidden = YES;
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"image name"]];
imageView.userInteractionEnabled = YES; // here to enable touch event
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleTapGestureRecongizer:)]; // handleTapGestureRecongizer is method will call when tap even fire
[imageView addGestureRecognizer:tap]; // Add Tap gesture recognizer to image view
[tap release], tap = nil;
[self.view addSubview:imageView];
[imageView release], imageView = nil;
Method handlerTapGestureRecognizer:
- (void)handleTapGestureRecongizer:(UITapGestureRecognizer *)gestureRecognizer{
if (gestureRecognizer.state == UIGestureRecognizerStateEnded) {
btn.hidden = NO;
}
}
have fun!