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?
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 tried to add reflection to my icarousel. Firstly my code was like this and it was working wright.
- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index reusingView:(UIView *)view
{
UIButton* button = (UIButton *)view;
if (button == nil)
{
//no button available to recycle, so create new one
UIImage *image = [arrKitapKapaklari objectAtIndex:index];
button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0.0f, 0.0f, image.size.width, image.size.height);
[button setTitleColor:[UIColor clearColor] forState:UIControlStateNormal];
[button setBackgroundImage:image forState:UIControlStateNormal];
button.titleLabel.font = [button.titleLabel.font fontWithSize:50];
[button addTarget:self action:#selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
}
//set button label
[button setTitle:[NSString stringWithFormat:#"%i", index] forState:UIControlStateNormal];
return button;
}
After changing, my code became like this:
- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index reusingView:(ReflectionView *)view
{
UIButton* button = nil;
if (button == nil)
{
view = [[[ReflectionView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 150.0f, 200.0f)] autorelease];
//no button available to recycle, so create new one
UIImage *image = [arrKitapKapaklari objectAtIndex:index];
button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0.0f, 0.0f, image.size.width, image.size.height);
[button setTitleColor:[UIColor clearColor] forState:UIControlStateNormal];
[button setBackgroundImage:image forState:UIControlStateNormal];
button.titleLabel.font = [button.titleLabel.font fontWithSize:50];
[button addTarget:self action:#selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
[view addSubview:button];
}
//set button label
[button setTitle:[NSString stringWithFormat:#"%i", index] forState:UIControlStateNormal];
[view update];
return view;
}
Reflection appears. But the problem is when i move the carousel view, images not flowing correctly. Sometimes another carousel view appears.
Do you have any idea what causes that?
Code should be:
- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index reusingView:(ReflectionView *)view
{
UIButton* button = nil;
if (view == nil)
{
//no view available to recycle, so create new one
view = [[[ReflectionView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 150.0f, 200.0f)] autorelease];
button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = view.bounds;
[button setTitleColor:[UIColor clearColor] forState:UIControlStateNormal];
button.titleLabel.font = [button.titleLabel.font fontWithSize:50];
[button addTarget:self action:#selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
button.tag = 99;
[view addSubview:button];
}
else
{
button = (UIButton *)[view viewWithTag:99];
}
//set button label
[button setTitle:[NSString stringWithFormat:#"%i", index] forState:UIControlStateNormal];
UIImage *image = [arrKitapKapaklari objectAtIndex:index];
[button setBackgroundImage:image forState:UIControlStateNormal];
[view update];
return view;
}
I use images with reflection built in them... don't need to do anything in code that way.. :p
I found my mistake. I am loading images here:
-(void) getImagesWithThread{
NSAutoreleasePool *pool;
pool = [[NSAutoreleasePool alloc] init];
//for(int i=0;i<bookCount;i++){
for(int i=0;i<bookCount;i++){
NSData * imageData = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString: [arrBookImages objectAtIndex:i]]];
UIImage *image = [[UIImage imageWithData: imageData] retain];
image = [self scaleToSize:CGSizeMake(140, 200) withImage:image];
if(image!=nil) [arrBookImages replaceObjectAtIndex:i withObject:image];
[image release]; }
[carousel reloadData];
[pool drain];
}
When i performed this in background it didn't reload correctly. İf i perform normally, it works. But i want to load them in a thread. I mean first i want to set images and replace them images from url. but i want them to appear one by one. Do you have any idea?
im running the same piece of code over and over when the user presses a button. If the user presses the button an image appears over the button, if they press it again the image is removed from the buttons subview.
here is my code:
UIImageView *overlay = [[UIImageView alloc]initWithImage:[UIImage imageNamed:#"OverlayImage.png"]];
NSString *senderstag = [NSString stringWithFormat:#"%i", buttonFromSender.tag];
if([[dictonaryOfImagesToDelete allKeys] containsObject:senderstag]){
[[buttonFromSender subviews]makeObjectsPerformSelector:#selector(removeFromSuperview)];
}
else{
[buttonFromSender addSubview:overlay];
}
NSString *imageName = [[NSString alloc]init];
imageName = [arrayWithImageNames objectAtIndex:buttonFromSender.tag];
[imagesToDelete addObject:imageName];
[dictonaryOfImagesToDelete setObject:imagesToDelete forKey:[NSString stringWithFormat:#"%i", buttonFromSender.tag]];
Im using a dictionary to keep track of which buttons have been pressed. instead of the imageview over the button being removed, it removes the whole button. How can i get it just to remove the imageview and not the button?
Thanks :D
I have ended up making the UIImageview of the overlay, a UIButton. This allowed to set actions for it. Here is the code that is used:
UIImage *image = [UIImage imageNamed:#"OverlayImage.png"];
UIButton *overlay = [[UIButton alloc]init];
overlay.frame = CGRectMake(0, 0, 100, 150);
[overlay setImage:image forState:UIControlStateNormal];
[overlay addTarget:self action:#selector(removeSelectedImage:) forControlEvents:UIControlEventTouchUpInside];
overlay.tag = buttonFromSender.tag;
[buttonFromSender addSubview:overlay];
NSString *imageName = [[NSString alloc]init];
imageName = [arrayWithImageNames objectAtIndex:buttonFromSender.tag];
[imagesToDelete addObject:imageName];
[dictonaryOfImagesToDelete setObject:imageName forKey:[NSString stringWithFormat:#"%i", buttonFromSender.tag]];
this set up the new UIButton
then to get rid of the UIButton i did this:
-(IBAction)removeSelectedImage:(id)sender{
UIButton *button = (UIButton *)sender;
[button removeFromSuperview];
NSString *tag = [NSString stringWithFormat:#"%i", button.tag];
NSString *deleteWhat = [[NSString alloc]init];
deleteWhat = [dictonaryOfImagesToDelete objectForKey:tag];
[imagesToDelete removeObject:deleteWhat];
[dictonaryOfImagesToDelete removeObjectForKey:tag];
}
If you want to display different image for a button state u can directly initialize the button with to load a picture in that state.
say for example Enabled/Disabled, or Selected/Deselected(normal)
UIButton* myButton = [UIButton buttonWithType:UIButtonTypeCustom];
[myButton setImage:[UIImage imageNamed:#"My Image"] forState:UIControlStateNormal];
[myButton setImage:[UIImage imageNamed:#"My Other Image"] forState:UIControlStateSelected];
[myButton setBackgroundImage:[UIImage imageNamed:#"BG Img"] forState:UIControlStateNormal];
References:
Background Image:
http://developer.apple.com/library/IOS/documentation/UIKit/Reference/UIButton_Class/UIButton/UIButton.html#//apple_ref/occ/instm/UIButton/backgroundImageForState:
Button Image:
http://developer.apple.com/library/IOS/documentation/UIKit/Reference/UIButton_Class/UIButton/UIButton.html#//apple_ref/occ/instm/UIButton/setImage:forState:
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.
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;