I have created a custom Facebook Login Button. Everything works but I can't get the image to center (top/bottom) in the button. The image it self is but when I create this button the image is lower.
//FB Button (Uses Facebook SDK)
self.fbLoginButton = [[FBLoginView alloc] init];
for (id loginObject in self.fbLoginButton.subviews)
{
if ([loginObject isKindOfClass:[UIButton class]])
{
UIButton * loginButton = loginObject;
UIImage *loginImage = [UIImage imageNamed:#"Facebook_White.png"];
[loginButton setBackgroundImage:loginImage forState:UIControlStateNormal];
[loginButton setBackgroundImage:nil forState:UIControlStateSelected];
[loginButton setBackgroundImage:nil forState:UIControlStateHighlighted];
[loginButton setTitle:nil forState:UIControlStateNormal];
[loginButton setTitle:nil forState:UIControlStateSelected];
[loginButton setTitle:nil forState:UIControlStateHighlighted];
}
if ([loginObject isKindOfClass:[UILabel class]])
{
UILabel * loginLabel = loginObject;
loginLabel.text = #"";
loginLabel.frame = CGRectMake(0, 0, 0, 0);
}
}
self.fbLoginButton.delegate = self;
self.fbLoginButton.readPermissions = #[#"public_profile", #"email"];
[[self.fbLoginButton layer]setBorderWidth:2.0];
[[self.fbLoginButton layer]setBorderColor:UIColorFromRGB(0xbbffd6).CGColor];
[buttonsArray addObject:self.fbLoginButton];
Here is what it looks like compared to the Twitter Button:
Here is the actual .png (the text is white so you can't see it unless it is against a background:
Any suggestions?
After playing around, this is what worked for me. Had to hard code the fbLoginView.frame and the loginButton.frame.
//FB Button (Uses Facebook SDK)
self.fbLoginView = [[FBLoginView alloc] init];
self.fbLoginView.readPermissions = #[#"public_profile", #"email"];
self.fbLoginView.frame = CGRectMake(0, 0, 35, 35);
[[self.fbLoginView layer]setBorderWidth:2.0];
[[self.fbLoginView layer]setBorderColor:UIColorFromRGB(0xbbffd6).CGColor];
for (id loginObject in self.fbLoginView.subviews)
{
if ([loginObject isKindOfClass:[UIButton class]])
{
UIButton * loginButton = loginObject;
loginButton.frame = CGRectMake(self.fbLoginView.frame.origin.x, self.fbLoginView.frame.origin.y, 35, 35);
UIImage *loginImage = [UIImage imageNamed:#"Facebook_White.png"];
[loginButton setBackgroundImage:loginImage forState:UIControlStateNormal];
[loginButton setBackgroundImage:nil forState:UIControlStateSelected];
[loginButton setBackgroundImage:nil forState:UIControlStateHighlighted];
}
if ([loginObject isKindOfClass:[UILabel class]])
{
UILabel * loginLabel = loginObject;
loginLabel.text = #"";
loginLabel.frame = CGRectMake(0, 0, 0, 0);
}
}
self.fbLoginView.delegate = self;
[socialButtonsArray addObject:self.fbLoginView];
Related
I have added FbLike button which provided in SDK 3.19.Its working fine but I need to change the image and text of button.I have googled but did not find anything.
Yes, this is possible the same way you would change the default FBLoginView button.
loginView.frame = CGRectMake(320/2 - 93/2, self.view.frame.size.height -200, 93, 93);
for (id loginObject in loginView.subviews)
{
if ([loginObject isKindOfClass:[UIButton class]])
{
UIButton * loginButton = loginObject;
UIImage *loginImage = [UIImage imageNamed:#"YOUR_IMAGE_HERE"];
loginButton.alpha = 0.7;
[loginButton setBackgroundImage:loginImage forState:UIControlStateNormal];
[loginButton setBackgroundImage:nil forState:UIControlStateSelected];
[loginButton setBackgroundImage:nil forState:UIControlStateHighlighted];
[loginButton sizeToFit];
}
if ([loginObject isKindOfClass:[UILabel class]])
{
UILabel * loginLabel = loginObject;
loginLabel.text = #"";
loginLabel.frame = CGRectMake(0, 0, 0, 0);
}
}
I am created two buttons that are right next to each other to mimic a segmented control. I am doing this to customize the appearance beyond what the UIKit allows. I decided to use the selected property to keep a button pressed. I have two images that one for each state normal and selected.
The problem is that when I select a button, the button highlights and turns dark, because of the hightlight state. I decided to use the selected image for the highlight state too, but it flashes, any ideas or suggestions.
- (void)leftSegmentPressed:(id)sender
{
if ([sender isSelected]) {
[sender setSelected:NO];
}
else {
[sender setSelected:YES];
}
}
For the "selected" button, disable it and manually switch the image for the state.
- (void) viewDidLoad
{
[rightSegmentButton setImage:[UIImage imageNamed:#"unselected.png"] forState:UIControlStateNormal];
[rightSegmentButton setImage:[UIImage imageNamed:#"selected.png"] forState:UIControlStateDisabled];
[leftSegmentButton setImage:[UIImage imageNamed:#"unselected.png"] forState:UIControlStateNormal];
[leftSegmentButton setImage:[UIImage imageNamed:#"selected.png"] forState:UIControlStateDisabled];
}
- (void)leftSegmentPressed:(id)sender
{
sender.enabled = NO;
rightSegmentButton.enabled = YES;
}
- (void)rightSegmentPressed:(id)sender
{
sender.enabled = NO;
leftSegmentButton.enabled = YES;
}
Check whether the image you given is in your Bundle or check image name you given is in lower case or not. Then write like
[button1 setImage:[UIImage imageNamed:#"normal1.png"] forState:UIControlStateNormal];
[button1 setImage:[UIImage imageNamed:#"selected1.png"] forState:UIControlStateSelected];
[button2 setImage:[UIImage imageNamed:#"normal2.png"] forState:UIControlStateNormal];
[button2 setImage:[UIImage imageNamed:#"selected2.png"] forState:UIControlStateSelected];
button1.tag = 1;
button2.tag = 2;
[button1 addTarget:self action:#selector(buttonSelected:) forControlEvents:UIControlEventTouchUpInside]
[button2 addTarget:self action:#selector(buttonSelected:) forControlEvents:UIControlEventTouchUpInside]
in your button event method
-(void)buttonSelected:(id)sender {
if([sender tag] == 1) {
button1.selected = YES;
button2.selected = NO;
} else {
button1.selected = NO;
button2.selected = YES;
}
}
[button setAdjustsImageWhenHighlighted:NO];
This will prevent the flicker.
UIButton *yourButton1 = [[UIButton buttonWithType:UIButtonTypeRoundedRect] retain];
yourButton1.frame = CGRectMake(110.0, 360.0, 100.0, 30.0);
[yourButton1 setTitle:#"Left" forState:UIControlStateNormal];
yourButton.backgroundColor = [UIColor clearColor];
yourButton1.tag = 1;
[yourButton1 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal ];
UIImage *buttonImageNormal = [UIImage imageNamed:#"yourNormalImage.png"];// set normal image
UIImage *strechableButtonImageNormal = [buttonImageNormal stretchableImageWithLeftCapWidth:12 topCapHeight:0];
[yourButton1 setBackgroundImage:strechableButtonImageNormal forState:UIControlStateNormal];
UIImage *buttonImagePressed = [UIImage imageNamed:#"yourSelectedImage.png"];// set selected image
UIImage *strechableButtonImagePressed = [buttonImagePressed stretchableImageWithLeftCapWidth:12 topCapHeight:0];
[yourButton1 setBackgroundImage:strechableButtonImagePressed forState:UIControlStateHighlighted];
[yourButton1 addTarget:self action:#selector(leftSegmentPressed:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:yourButton1];
do same for another second button.
and in action method set bellow code..
- (void)leftSegmentPressed:(id)sender
{
UIButton *btnTemp = (UIBUtton *)sender;
if (btnTemp.tag == 1) {
[yourButton1 setSelected:YES];
[yourButton2 setSelected:NO];
}
else {
[yourButton1 setSelected:NO];
[yourButton2 setSelected:YES];
}
}
Im really having a problem with my image with button frame,
What I want is:
1. When it is selected it will have a checkmark
2. When the view is dismissed then returned back to the previous screen it will still have the check in the image selected
3. When I tap the selected image or another image it will remove the checkmark.
I was able to do it but I'm having problem with the setting of image:
What I want is this:
UIImage *bgImage = [UIImage imageNamed:#"AGIPC-Checkmark-iPhone.png"];
[myButton setBackgroundImage:bgImage forState:UIControlStateDisabled];
[myButton setEnabled:NO];
But this is the function I needed:
UIImage *bgImage = [UIImage imageNamed:#"AGIPC-Checkmark-iPhone.png"];
[myButton setImage:bgImage forState:UIControlStateSelected];
[myButton setSelected:YES];
Here is the code below(If there is something wrong with what I'm doing please tell me, Thanks):
- (void)viewDidLoad {
UIImage *bgImage = [UIImage imageNamed:#"AGIPC-Checkmark-iPhone.png"];
[myButton setBackgroundImage:bgImage forState:UIControlStateHighlighted];
[myButton setBackgroundImage:bgImage forState:UIControlStateDisabled];
// Create view
UIScrollView *view = [[UIScrollView alloc] initWithFrame:CGRectMake(0.0f,0.0f,1024.0f,768.0f)];
int row = 0;
int column = 0;
for(int i = 0; i < _thumbs.count; ++i) {
UIImage *thumb = [_thumbs objectAtIndex:i];
myButton = [UIButton buttonWithType:UIButtonTypeCustom];
myButton.frame = CGRectMake(column*140+24, row*150+10, 100, 100);
[myButton setImage:thumb forState:UIControlStateNormal];
[myButton addTarget:self
action:#selector(buttonClicked:)
forControlEvents:UIControlEventTouchUpInside];
myButton.tag = i;
NSLog(#"%i",i);
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
NSInteger selectedImageSlot1 = [prefs integerForKey:#"selected1"];
if ( selectedImageSlot1 == i){
[self turnButtonToSelected];
}
[view addSubview:myButton];
if (column == 6) {
column = 0;
row++;
} else {
column++;
}
}
[view setContentSize:CGSizeMake(1024, (row+1) * 150 + 10)];
}
- (void)turnButtonToSelected
{ UIImage *bgImage = [UIImage imageNamed:#"AGIPC-Checkmark-iPhone.png"];
[myButton setBackgroundImage:bgImage forState:UIControlStateNormal];
[myButton setBackgroundImage:bgImage forState:UIControlStateHighlighted];
[myButton setBackgroundImage:bgImage forState:UIControlStateDisabled];
[myButton setEnabled:NO];
}
- (void)highlightButton:(UIButton *)a {
//[self turnButtonToSelected];
}
- (IBAction)buttonClicked:(id)sender {
myButton = (UIButton *)sender;
self.selectedImage = [_images objectAtIndex:myButton.tag];
if (myButton.selected) {
[sender setSelected:NO];
}else {
UIImage *bgImage = [UIImage imageNamed:#"AGIPC-Checkmark-iPhone.png"];
[sender setImage:bgImage forState:UIControlStateSelected];
[sender setSelected:YES];
}
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
[prefs setInteger:myButton.tag forKey:#"slot1"];
[[NSUserDefaults standardUserDefaults] setInteger:myButton.tag forKey:#"selected1"];
[[NSUserDefaults standardUserDefaults] synchronize];
[self performSelector:#selector(highlightButton:) withObject:sender afterDelay:0.0];
[self dismissModalViewControllerAnimated:NO];
}
Or is there a way to forcefully make a button enabled whenever it is disabled when tapped also?
I have just tried this on a different approach.
For me it works just fine and achieves your effect.
Let me know if you need further explanation or the entire Xcode project.
- (void)viewDidLoad
{
[super viewDidLoad];
//button 1
UIButton *imageButton = [UIButton buttonWithType:UIButtonTypeCustom];
imageButton.frame = CGRectMake(30, 50, 100, 80);
UIImage *btnImage = [UIImage imageNamed:#"DSC_5359.JPG"];
[imageButton setBackgroundImage:btnImage forState:UIControlStateNormal];
[imageButton addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchDown];
//button 2
UIButton *imageButton2 = [UIButton buttonWithType:UIButtonTypeCustom];
imageButton2.frame = CGRectMake(180, 50, 100, 80);
UIImage *btnImage2 = [UIImage imageNamed:#"DSC_5355.JPG"];
[imageButton2 setBackgroundImage:btnImage2 forState:UIControlStateNormal];
[imageButton2 addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchDown];
[self.view addSubview:imageButton];
[self.view addSubview:imageButton2];
}
- (void)buttonClicked:(UIButton*)button
{
//NSLog(#"Button clicked! %#", button);
UIImage *checkmarkImage = [UIImage imageNamed:#"check.png"];
UIImage *empty = nil;
//NSLog(#"Imageview: %d", button.imageView.tag);
if (button.imageView.tag == 0)
{
[button setImage:checkmarkImage forState:UIControlStateNormal];
button.imageView.tag = 1;
}
else
{
[button setImage:empty forState:UIControlStateNormal];
button.imageView.tag = 0;
}
}
Best, Chris
Update: I have updated the code. Here is the entire ViewController. Note: I would solve this using coreData to store the current index. The following code is not the best to solve this issue but is based on your initial code!
//
// SOViewController.m
// SOImageOverlay
//
// Created by Chris on 24.07.12.
//
#import "SOViewController.h"
#interface SOViewController ()
#end
#define CHECKMARK_IMAGE #"check.png"
#implementation SOViewController
#synthesize buttons = _buttons;
#pragma mark - Custom getter
- (NSMutableArray*)buttons
{
if (_buttons == nil)
_buttons = [[NSMutableArray alloc] init];
return _buttons;
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
// UIImage *bgImage = [UIImage imageNamed:CHECKMARK_IMAGE];
// UIButton *myButton =
// [myButton setBackgroundImage:bgImage forState:UIControlStateHighlighted];
// [myButton setBackgroundImage:bgImage forState:UIControlStateDisabled];
// Create view
// UIScrollView *view = [[UIScrollView alloc] initWithFrame:CGRectMake(0.0f,0.0f,1024.0f,768.0f)];
int row = 0;
int column = 0;
for(int i = 0; i < 2; ++i) {
/*UIImage *thumb = [_thumbs objectAtIndex:i];
myButton = [UIButton buttonWithType:UIButtonTypeCustom];
myButton.frame = CGRectMake(column*140+24, row*150+10, 100, 100);
[myButton setImage:thumb forState:UIControlStateNormal];
[myButton addTarget:self
action:#selector(buttonClicked:)
forControlEvents:UIControlEventTouchUpInside];
myButton.tag = i;
NSLog(#"%i",i);*/
UIButton *imageButton = [UIButton buttonWithType:UIButtonTypeCustom];
if (i == 0)
{
UIImage *btnImage = [UIImage imageNamed:#"DSC_5359.JPG"];
imageButton.frame = CGRectMake(30, 50, 100, 80);
[imageButton setBackgroundImage:btnImage forState:UIControlStateNormal];
}
else if (i == 1)
{
UIImage *btnImage = [UIImage imageNamed:#"DSC_5355.JPG"];
imageButton.frame = CGRectMake(180, 50, 100, 80);
[imageButton setBackgroundImage:btnImage forState:UIControlStateNormal];
}
[imageButton addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchDown];
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
NSInteger selectedImageSlot1 = [prefs integerForKey:#"selected1"];
if ( selectedImageSlot1 == i){
[self turnButtonToSelected:imageButton];
}
[self.view addSubview:imageButton];
//adding current button into buttons array
[self.buttons addObject:imageButton];
if (column == 6) {
column = 0;
row++;
} else {
column++;
}
}
//[view setContentSize:CGSizeMake(1024, (row+1) * 150 + 10)];
// //button 1
// UIButton *imageButton = [UIButton buttonWithType:UIButtonTypeCustom];
// imageButton.frame = CGRectMake(30, 50, 100, 80);
// UIImage *btnImage = [UIImage imageNamed:#"DSC_5359.JPG"];
// [imageButton setBackgroundImage:btnImage forState:UIControlStateNormal];
// [imageButton addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchDown];
//
// //button 2
// UIButton *imageButton2 = [UIButton buttonWithType:UIButtonTypeCustom];
// imageButton2.frame = CGRectMake(180, 50, 100, 80);
// UIImage *btnImage2 = [UIImage imageNamed:#"DSC_5355.JPG"];
// [imageButton2 setBackgroundImage:btnImage2 forState:UIControlStateNormal];
// [imageButton2 addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchDown];
//
// [self.view addSubview:imageButton];
// [self.view addSubview:imageButton2];
}
//- (void)buttonClicked:(UIButton*)button
//{
// //NSLog(#"Button clicked! %#", button);
//
// UIImage *checkmarkImage = [UIImage imageNamed:#"check.png"];
// UIImage *empty = nil;
//
// //NSLog(#"Imageview: %d", button.imageView.tag);
// if (button.imageView.tag == 0)
// {
// [button setImage:checkmarkImage forState:UIControlStateNormal];
// button.imageView.tag = 1;
// }
// else
// {
// [button setImage:empty forState:UIControlStateNormal];
// button.imageView.tag = 0;
// }
//}
// code from stackoverflow
// ------------------------------------------------------------
- (void)turnButtonToSelected:(UIButton*)button
{
UIImage *bgImage = [UIImage imageNamed:CHECKMARK_IMAGE];
//UIButton *myButton = button; //just to use the copied code
button.selected = YES;
[button setImage:bgImage forState:UIControlStateSelected];
// [myButton setBackgroundImage:bgImage forState:UIControlStateNormal];
// [myButton setBackgroundImage:bgImage forState:UIControlStateHighlighted];
// [myButton setBackgroundImage:bgImage forState:UIControlStateDisabled];
// [myButton setEnabled:NO];
}
- (void)highlightButton:(UIButton *)a {
//[self turnButtonToSelected];
}
- (IBAction)buttonClicked:(id)sender
{
UIButton *myButton = (UIButton *)sender;
//I have removed this line... due to the fact that I don't want to
//implement your arrays.
//self.selectedImage = [_images objectAtIndex:myButton.tag];
//deselecting all
for (UIButton *button in self.buttons)
{
button.selected = NO;
//[button setImage:nil forState:UIControlStateNormal];
}
if (myButton.selected) {
[sender setSelected:NO];
}else {
UIImage *bgImage = [UIImage imageNamed:CHECKMARK_IMAGE];
[sender setImage:bgImage forState:UIControlStateSelected];
[sender setSelected:YES];
}
// NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
// [prefs setInteger:myButton.tag forKey:#"slot1"];
//getting index of button in array
NSInteger index = 0;
NSInteger foundIndex = -1;
for (UIButton *button in self.buttons)
{
if (button == myButton)
{
foundIndex = index;
}
index++;
}
if (foundIndex >= 0)
{
[[NSUserDefaults standardUserDefaults] setInteger:foundIndex forKey:#"selected1"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
[self performSelector:#selector(highlightButton:) withObject:sender afterDelay:0.0];
// [self dismissModalViewControllerAnimated:NO];
}
// ------------------------------------------------------------
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
- (IBAction)closeButtonClicked:(UIBarButtonItem *)sender
{
[self dismissModalViewControllerAnimated:YES];
}
#end
Try doing this.
UIImage *bgImage = [UIImage imageNamed:#"AGIPC-Checkmark-iPhone.png"];
**[myButton setSelected:NO];
[myButton setBackgroundImage:bgImage forState:UIControlStateNormal];**
UIImage *bgImage = [UIImage imageNamed:#"AGIPC-Checkmark-iPhone.png"];
**[myButton setSelected:YES];
[myButton setImage:bgImage forState:UIControlStateSelected];**
In the later part of code, you are setting the state of the button to "selected" but in the upper code, you haven't. Rather, you have disabled the button. Try the above code which I have provided. Thanks.
Just add you chekedImage as a subview on top of the button when user clicks the button
UIImage *button = [UIImage imageNamed:#"cat.png"];
[myButton setBackgroundImage:bgImage forState:UIControlStateNormal];
//add target button clicked
-(void)buttonClicked : (id)sender{
if(toogleSwitch){
[button addSubview:checkedimageView]; // Just the checked image icon
}
else{
[imageview removeFromSuperview];
}
}
What you are trying to do will only work when you have
resources of two types for all your images.
1) The image itself
2) Image with checked box as transparent layer.
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?
I created array of buttons, but i am not see, that buttons handles touch events ( buttonEvent: not calls)
This my code - is it not correct ?
- (void)loadView{
CGRect screenRect = [[UIScreen mainScreen] applicationFrame];
UIImageView *backgroundImageView = [[UIImageView alloc] initWithFrame:screenRect];
[backgroundImageView setImage:[UIImage imageNamed:#"background.png"]];
backgroundImageView.opaque = YES;
self.view = backgroundImageView;
[backgroundImageView release];
CGRect brandRect = CGRectMake(90, 25, 140, 70);
UIImageView *brandImageView = [[UIImageView alloc] initWithFrame:brandRect];
[brandImageView setImage:[UIImage imageNamed:#"brand.png"]];
[self.view addSubview:brandImageView];
buttons = [NSMutableArray array];
int y = 100;
for ( int i = 0 ; i < 5; i++ ){
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setFrame:CGRectMake(20, y, 280, 50)];
if ( i == 0 ){
[button setBackgroundImage:[UIImage imageNamed:#"select_active.png"] forState:UIControlStateNormal];
} else {
[button setBackgroundImage:[UIImage imageNamed:#"select_passive.png"] forState:UIControlStateNormal];
}
[button setTitle:[NSString stringWithFormat:#"Object%d",i] forState:UIControlStateNormal];
[button setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
[button addTarget:self action:#selector(buttonEvent:) forControlEvents:UIControlEventAllEvents];
button.tag = 1000 + i ;
[self.view addSubview:button];
y += 60;
}
-(void)buttonEvent:(id)sender {
NSLog(#"new button clicked!!!");
}
You are adding buttons to uiimageview, which has interaction disabled.
Also, you don't have an array of buttons, because buttons is autoreleased, and you never add object into buttons array.