I am using following code:
slidingImageIconView=[[UIScrollView alloc]init];
[slidingImageIconScrollView setContentSize:CGSizeMake(110, 90)];
UIButton *iconImageSlide=[[UIButton alloc] init];
[iconImageSlide setImage:[UIImage imageNamed:#"dummy_img.jpg"] forState:UIControlStateNormal];
[slidingImageIconScrollView addSubview:iconImageSlide];
but my button is not adding in scroll view, i know i am missing something very silly but please help me..
I notice that you have slidingImageIconView and slidingImageIconScrollView. Is that a typo? I've left it like that in my examples here.
You need to initWithFrame in order to set the button position and size:
slidingImageIconView=[[UIScrollView alloc]init];
[slidingImageIconScrollView setContentSize:CGSizeMake(110, 90)];
UIButton *iconImageSlide=[[UIButton alloc] initWithFrame:CGRectMake(0,0,100,20)];
[iconImageSlide setImage:[UIImage imageNamed:#"dummy_img.jpg"] forState:UIControlStateNormal];
[slidingImageIconScrollView addSubview:iconImageSlide];
If you want to make the button the same size as the image - then the following rearrangement would allow that. Also note that I've used setBackgroundImage instead of setImage. I tend to use setImage to put an 'icon' on the button, but setBackgroundImage for the actual button-image.
UIImage* buttonImage = [UIImage imageNamed:#"dummy_img.jpg"];
slidingImageIconView=[[UIScrollView alloc]init];
[slidingImageIconScrollView setContentSize:CGSizeMake(110, 90)];
UIButton *iconImageSlide=[[UIButton alloc] initWithFrame:CGRectMake(0,0,buttonImage.size.width,buttonImage.size.height)];
[iconImageSlide setBackgroundImage:buttonImage forState:UIControlStateNormal];
[slidingImageIconScrollView addSubview:iconImageSlide];
You haven't set the frame of the buttonf it has no useful geometry.
Related
How to remove this space image here. Here is my code :
UIButton *postButton = [UIButton buttonWithType:UIButtonTypeCustom];
[postButton setImage:[UIImage imageNamed:#"post_btn.png"] forState:UIControlStateNormal];
postButton.frame = CGRectMake(0, 0, 44, 44);
[postButton addTarget:self.viewDeckController action:#selector(postAction:) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:postButton];
thx for help.
As standard, you can't. And the cost of doing so is likely to be prohibitive as you would need to create your own custom navigation bar (or subclass) and manage the drawing yourself.
That said, the gap is there for a reason as touch detection isn't so accurate near the edge of the screen and fingers are big...
First there is no need to use UIButton but we can. you can use it UIBarButtonItem
UIImage *myImage = [UIImage imageNamed:#"post_btn.png"];
UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithImage:myImage style:UIBarButtonItemStyleBordered target:self action:#selector(postAction:)];
self.navigationItem.rightBarButtonItem = button;
[button release];
As standard, you can't. but you can set manually.
1- setNavigationBarHidden hidden.
[self.navigationItem setNavigationBarHidden:TRUE];
2- Take one UIImageView size = 320 X 45. //as per your required height
3- set your button on it. make it height smaller then above image.
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
I have written this code to see different image states...
UIButton *btnComment = [UIButton buttonWithType:UIButtonTypeCustom];
btnComment.tag=indexPath.row;
[btnComment addTarget:self action:#selector(goToComment:)forControlEvents:UIControlEventTouchDown];
UIImage *img1 = [UIImage imageNamed:#"commentbtndown.png"];
UIImage *img2 = [UIImage imageNamed:#"commentbtnup.png"];
UIImage *img3 = [UIImage imageNamed:#"commentbtnover.png"];
[btnComment setImage:img1 forState:UIControlStateNormal];
[btnComment setImage:img2 forState:UIControlStateHighlighted];
[btnComment setImage:img3 forState:UIControlStateSelected];
[btnComment setImage:img2 forState:(UIControlStateHighlighted+UIControlStateSelected)];
btnComment.frame =CGRectMake(0, 100, 95, 25);
[cell addSubview:btnComment];
[img1 release];
[img2 release];
[img3 release];
but its not working, it is always showing me image 1.
p.s. I have added these images in the table view cell
The problem is that you are creating the UIImage objects with an autorelease method imageNamed, and you are releasing these objects afterwards, which cause your button to have invalid objects and because of that the images will not be displayed
Try removing this lines of code and your button will work
[img1 release];
[img2 release];
[img3 release];
And also, if you want the button to receive the touch events, you will have to add it to the contentView of your cell object, otherwise the button will be shown but you will not be able to tap it.
[cell.contentView addSubview:btnComment]
Well one problem with your code is that you should not be releasing those image variables. imageNamed: returns an autoreleased UIImage. I'm doubt that this is causing your problem, though.
Try using | instead of + for your fourth setImage call.
Another problem with your code is where you add the button to your UITableViewCell. Instead of this:
[cell addSubview:btnComment];
You should add subviews to your cell's contentView:
[cell.contentView addSubview:btnComment];
But I'm also not certain that this may cause your problem...
how can i customize the navigation bar or add the background image in it and buttons of navigation bar?
please tell me in simple way not make the answer hard to understand i am not expert and?
explain step by step.
Follow this tutorial: How do iPhone apps Instagram/Reeder/DailyBooth implement custom NavigationBars with variable width back buttons?. It includes a source code example.
What you're looking for sadly isn't that easy to accomllish. The technique required rather reminds of a workaround than a working solution It's called method swizzling and here is a tutorial on how to apply it on a navbar in order to get a custom background image in there: http://sebastiancelis.com/2009/12/21/adding-background-image-uinavigationbar/
Adding buttons to your navigationbar is fairly easy. Say your navigation bar is instansiated as navigationController:
UIBarButtonItem *yourButton = [[UIBarButtonItem alloc] initWithTitle:#"Hello" style: UIBarButtonStylePlain target:self action:#selector(myTargetMethodSignature:)];
navigationController.navigationItem.rightBarButtonItem = yourButton;
[yourButton release];
Hope this helps.
Wasabi
Add the code in .m file above #implementation
- (void)drawRect:(CGRect)rect {
/ UIImage *image = [UIImage imageNamed: #"navigation_bar.png"];
UIImage *image = [UIImage imageNamed: #"navigationbarnew2.png"];
[image drawInRect:CGRectMake(0, 0, self.frame.size.width, 44)];
}
#end
And to add button to navigation bar add the following code in viewdidload
UIImage* image3 = [UIImage imageNamed:#"mail-48_24.png"];
CGRect frameimg = CGRectMake(0, 0, image3.size.width, image3.size.height);
UIButton *someButton = [[UIButton alloc] initWithFrame:frameimg];
[someButton setBackgroundImage:image3 forState:UIControlStateNormal];
[someButton addTarget:self action:#selector(sendmail)
forControlEvents:UIControlEventTouchUpInside];
[someButton setShowsTouchWhenHighlighted:YES];
mailbutton =[[UIBarButtonItem alloc] initWithCustomView:someButton];
self.navigationItem.rightBarButtonItem=mailbutton;
[someButton release];
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.