I have created a button using code shown below -
UIImage *kalenderImage = [UIImage imageNamed:#"start_icon_calendar_u.png"];
UIImageView *kalenderImageView = [[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, kalenderImage.size.width/2, kalenderImage.size.height/2)] autorelease];
[kalenderImageView setImage:kalenderImage];
UILabel* kalendarLabel = [[[UILabel alloc] initWithFrame:CGRectMake (0, kalenderImage.size.height/2-15, kalenderImage.size.width/2, kalenderImage.size.height/2)] autorelease];
kalendarLabel.text = #"Kalender";
[kalenderButton addSubview:kalenderImageView];
[kalenderButton addSubview:kalendarLabel];
[kalenderButton addTarget:self action:#selector(showCalendar:) forControlEvents:UIControlEventTouchUpInside];
I need to change the image of button for UIControlStateHighlighted state. How can i do this ?
I dont want to use
KalenderButton setBackgroundImage:#"" forState:]
[KalenderButton setImage:#"" forState]
Its very simple. Look at the following code. It checks the file is locked and changes the image of the button accordingly. You can edit the code according to your necessity.
if (fileLocked) {
UIImage *image = [UIImage imageNamed: [NSString stringWithFormat:#"icon-lock.png"]];
[lockButton setImage:image forState:UIControlStateNormal];
} else {
UIImage *image = [UIImage imageNamed: [NSString stringWithFormat:#"icon-unlock.png"]];
[lockButton setImage:image forState:UIControlStateNormal];
}
It works.
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];
Whats wrong with the following piece of code:
UIButton *button = [[UIButton alloc]init];
CGRect frame = CGRectMake(180, 10, 33, 33);
button.frame = frame;
button.tag = 1001;
UIImage *image = [[[UIImage alloc]init]autorelease];
image = [UIImage imageNamed:#"icon.png"];
[button setBackgroundImage:image forState:UIControlStateNormal];
[image release];
[button release];
If this is wrong where does it need correction and why?
I see several problems:
You sent autorelease to image, then manually released it.
You released button, but you didn't add it as a subview to anything. So basically, you did all that for nothing.
You instantiate UIImage, then you do instantiate it again. in the next line: Instead just do:
UIImage *image = [UIImage imageNamed:#"icon.png"];
and delete UIImage *image = [[[UIImage alloc]init]autorelease]; and [image release]; statement.
You can try this
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(180, 10, 33, 33);;
button.tag = 1001;
UIImage *image = [UIImage imageNamed:#"icon.png"];
if (image == nil) {
NSLog(#"can't find icon.png");
} else {
[button setBackgroundImage:image forState:UIControlStateNormal];
}
//add button to the parent's view
Did you make sure image is not nil?
How can I create a slider in customized slider in iPhone. Currently I am using the slider selected from "xib"(Interface Builder) but it is showing me normal slider.But I need slider with customized with my own button and showing text
Slide To Start
How can I do this please help me out .
See following links
http://www.applausible.com/blog/?p=250
http://iphone4developer.blogspot.com/2010/12/custom-uislider.html
Change iPhone UISlider bar image
http://jainmarket.blogspot.com/2010/06/customize-default-uislider-in-iphone.html
http://www.xprogress.com/post-35-uislider-tutorial-example-how-to-use-slider-in-iphone-sdk-xcode/
http://www.obsessivecode.com/projects/ocprogress/
you can accomplish like
UISlider *mySlider;
mySlider = [[UISlider alloc]init];
/* Slider Customization */
[mySlider setThumbImage:[UIImage imageNamed:#"yourimage.png"] forState:UIControlStateNormal];
/*the above line will sets a custom image for your slider ball i.e; thumb image*/
/* the below code will make your slider line custom */
UIImage *sliderLeftTrackImage = [[UIImage imageNamed: #"lineImage.png"] stretchableImageWithLeftCapWidth: 9 topCapHeight: 0];
UIImage *sliderRightTrackImage = [[UIImage imageNamed: #"lineImage.png"] stretchableImageWithLeftCapWidth: 9 topCapHeight: 0];
[mySlider setMinimumTrackImage: sliderLeftTrackImage forState: UIControlStateNormal];
[mySlider setMaximumTrackImage: sliderRightTrackImage forState: UIControlStateNormal];
/* end of Slider Customization */
[mySlider release];
You will need to use stretchable images for the customization, and load them with the following methods:
setMinimumTrackImage:forState:
setMaximumTrackImage:forState:
setThumbImage:forState:
The minimum track image is the part on the left of the thumb (by default colored blue), and the maximum track image is on the right side.
Here is an example for the customization:
UIImage *minimumTrackImage = [UIImage imageNamed:#"sliderMin.png"];
UIImage *stretchableMinimumTrackImage = [minimumTrackImage stretchableImageWithLeftCapWidth:6 topCapHeight:6];
[slider setMinimumTrackImage:stretchableMinimumTrackImage forState:UIControlStateNormal];
UIImage *maximumTrackImage = [UIImage imageNamed:#"sliderMax.png"];
UIImage *stretchableMaximumTrackImage = [maximumTrackImage stretchableImageWithLeftCapWidth:6 topCapHeight:6];
[slider setMaximumTrackImage:stretchableMaximumTrackImage forState:UIControlStateNormal];
UIImage *handle = [UIImage imageNamed:#"sliderHandle.png"];
[slider setThumbImage:handle forState:UIControlStateNormal];
UISlider *objslider = [[UISlider alloc] initWithFrame:CGRectMake(14, 249, 290, 22)];
UIImage *stetchLeftTrack = [[UIImage imageNamed:#"slider_fill.png"] stretchableImageWithLeftCapWidth:0.0 topCapHeight:0.0];
UIImage *stetchRightTrack = [[UIImage imageNamed:#"slider.png"]
stretchableImageWithLeftCapWidth:0.0 topCapHeight:0.0];
[objslider setThumbImage: [UIImage imageNamed:#"slider_ball.png"]forState:UIControlStateNormal];
[objslider setMinimumTrackImage:stetchLeftTrack forState:UIControlStateNormal];
[objslider setMaximumTrackImage:stetchRightTrack forState:UIControlStateNormal];
objslider.minimumValue = 0.0;
objslider.maximumValue = 100.0;
objslider.continuous = YES;
objslider.value = 0.0;
[self.view addSubview:objslider];
I am using the following code to add a custom navbar to my app. But I want to be able to change the navbar depending on the view, is this possible?
#implementation UINavigationBar (UINavigationBarCategory)
- (void)drawRect:(CGRect)rect
{
UIColor *color = [UIColor colorWithRed:82/255.0 green:80/255.0 blue:81/255.0 alpha:1.0];
UIImage *img = [UIImage imageNamed: #"navbar.png"];
[img drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
self.tintColor = color;
}
#end
If not, is it possible to just add an image to the navbar instead of title text?
I would strongly discourage using a category to override -drawRect on a UINavBar as this design will break on a soon to be released iOS version.
It is very easy to create custom images and buttons to the navBar title though. Here's some code snippets I pulled directly out of production code for one of my apps. It creates a custom image button in the navBar's title area, but it's just as easy to create an image instead:
- (void) viewDidLoad {
UIButton *titleButton = [self buttonForTitleView];
[titleButton setTitle:newTitle forState:UIControlStateNormal];
[titleButton sizeToFit];
self.navigationBar.topItem.titleView = titleButton;
}
- (UIButton *) buttonForTitleView {
if (!_buttonForTitleView) {
UIImage *buttonImage = [UIImage imageNamed:#"button_collection_name2"];
UIImage *pressedButtonImage = [UIImage imageNamed:#"button_collection_name2_pressed"];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setBackgroundImage: buttonImage forState : UIControlStateNormal];
[button setBackgroundImage: pressedButtonImage forState : UIControlStateHighlighted];
[button setFrame:CGRectMake(0, 0, buttonImage.size.width, buttonImage.size.height)];
[button setTitleColor:[UIColor colorWithRed:38.0/255.0 green:38.0/255.0 blue:38.0/255.0 alpha:1.0] forState:UIControlStateNormal];
[button setTitleShadowColor:[UIColor colorWithRed:230.0/255.0 green:230.0/255.0 blue:230.0/255.0 alpha:1.0] forState:UIControlStateNormal];
button.titleLabel.adjustsFontSizeToFitWidth = YES;
button.titleLabel.minimumFontSize = 10.0;
button.titleEdgeInsets = UIEdgeInsetsMake(0.0, 10.0, 0.0, 10.0);
button.titleLabel.shadowOffset = (CGSize){ 0, 1 };
[button addTarget:self action:#selector(presentWidgetCollectionSwitchingViewController:) forControlEvents:UIControlEventTouchUpInside];
_buttonForTitleView = [button retain];
}
return _buttonForTitleView;
}
And this is what the button looks like in the app VideoBot:
Here's an example of using an image for the navbar title:
UIImage *titleImage = [UIImage imageNamed:#"navbar_title_image"];
UIImageView *titleImageView = [[[UIImageView alloc] initWithImage:titleImage] autorelease];
UIView *container = [[[UIView alloc] initWithFrame:(CGRect){0.0, 0.0, titleImage.size.width, titleImage.size.height}] autorelease];
container.backgroundColor = [UIColor clearColor];
[container addSubview:titleImageView];
// add the view to the title
self.navigationBar.topItem.titleView= container;
I have a simple button and I want to put an image inside, so I have this code:
myButton = [[UIButton alloc] initWithFrame:CGRectMake(20, 13, 116, 138)];
UIImage *image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[self.visuel lpath] ofType:#"jpg"]];
[myButton setImage:image forState:UIControlStateNormal];
in InterfaceBuilder Its easy to tell my image to take the size and width of my button but how to do
that programmatically? do I have to use myButton.contentHorizontalAlignment =..... something like that?
Best Regards,
What you probably want to do is this...
myButton = [[UIButton buttonWithType:UIButtonTypeCustom] retain];
myButton.frame = CGRectMake(20.0, 13.0, 116.0, 138.0);
UIImage *tImage = [[UIImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[self.visue1 lpath] ofType:#"jpg"]];
[myButton setBackgroundImage:[tImage stretchableImageWithLeftCapWidth:11 topCapHeight:0] forState:UIControlStateNormal];
[tImage release];
Of course the cap widths will depend on your image. It just repeats the next line of pixels over and over again until your image stretches to the proper width