how do I left align a button in UITableViewCell - objective-c

How do I align the table cell button to the left instead of the right? Below is the code I am using to create the UIButton. Thanks.
cell.accessoryView = [self getButton: #"icon.png"];
...
- (UIButton*)getButton: (NSString*)icon {
UIImage *image = (true) ? [UIImage imageNamed:icon] : [UIImage imageNamed:icon];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
CGRect frame = CGRectMake(0.0, 0.0, image.size.width, image.size.height);
button.frame = frame;
[button setBackgroundImage:image forState:UIControlStateNormal];
[button addTarget:self action:#selector(checkButtonTapped:event:) forControlEvents:UIControlEventTouchUpInside];
button.backgroundColor = [UIColor clearColor];
return button;
}

You are adding the button as an accessory view, which cannot be put on the left. See this question, it is almost exactly the same:
iOS UITableViewCell accessory on the left side?

Then you can muck with the frame of the button.
UIButton *cellButton = [self getButton:#"icon.png"];
[cell.contentView addSubview:cellButton];

Related

ChildView didn't maintain its ParentView Frame

Hope all of you will be fine. Guys i have a situation, I have a uiview and uibutton as under:
UIView* containerView = [[UIView alloc] initWithFrame:CGRectMake(0, 200, self.view.frame.size.width, 70)];
containerView.backgroundColor = [UIColor whiteColor];
[self.view addSubview:containerView];
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.backgroundColor = [UIColor redColor];
[button addTarget:self action:#selector(SettingPressed:) forControlEvents:UIControlEventTouchUpInside];
UIImage *image = [UIImage imageNamed:#"settings_button.png"];
[button setImage:image forState:UIControlStateHighlighted & UIControlStateNormal & UIControlStateSelected];
button.frame = CGRectMake(0,0, 40, 40);
button.center = containerView.center;
[containerView addSubview:button];
The problem is uibutton appear far from uiview instead of in the middle of uiview. What i am mistaking, please guide me.
Thanks.
First, when you change a view's center property the frame property will be changed automatically so you only should set the button center:
button.center = CGPointMake(containerView.frame.size.width / 2,
containerView.frame.size.height / 2);
Now, whenever you change the center property you need to manually tell the view to redraw itself using setNeedsDisplay :
[button setNeedsDisplay]
and lastly, the difference between frame and bounds that frame defines the position and size relative to the parent view while bounds describes the drawing area inside (or outside) the frame.

UIButton not working on animated subView

I want to add two buttons to a subview that appears over the parent view when the view will appear. The buttons are initialized correctly and appear but they donĀ“t react when pressed. Does anybody know why? Here is my code:
-(void) viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
self.view.backgroundColor = [UIColor blackColor];
//Animation View
imageView= [[UIImageView alloc] initWithFrame:CGRectMake(0, 499, 320, 0)];
imageView.image = [UIImage imageNamed:#"trans.png"];
[imageView setClipsToBounds:YES];
[imageView setContentMode:UIViewContentModeBottom];
[self.view addSubview:imageView];
[UIView animateWithDuration:1.0f
delay:0.5f
options:UIViewAnimationOptionCurveEaseOut
animations:^(void) {
imageView.frame = CGRectMake(0, 92, 320, 320);
}
completion:NULL];
[self.view bringSubviewToFront:imageView];
// Animation View Buttons
//1 Amazon
UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *button=[UIImage imageNamed:#"button.png"];
[button1 setImage:button forState:UIControlStateNormal];
button1.frame = CGRectMake(0, 290, 80, 30);
button1.backgroundColor=[UIColor whiteColor];
button1.showsTouchWhenHighlighted=YES;
[button1 addTarget:self
action:#selector(openAmazon:)
forControlEvents:UIControlEventTouchUpInside];
[imageView addSubview:button1];
//2 iTunes
UIButton *button2 = [UIButton buttonWithType:UIButtonTypeCustom];
[button2 setImage:button forState:UIControlStateNormal];
button2.frame = CGRectMake(82, 290, 80, 30);
button2.backgroundColor=[UIColor whiteColor];
button2.showsTouchWhenHighlighted=YES;
[button2 addTarget:self
action:#selector(openiTunes:)
forControlEvents:UIControlEventTouchUpInside];
[imageView addSubview:button2];
}
you need to use the option UIViewAnimationOptionAllowUserInteraction
...
options:UIViewAnimationOptionCurveEaseOut | UIViewAnimationOptionAllowUserInteraction
...
Make sure you set the userInteractionEnabled property on the imageView:
[imageView setUserInteractionEnabled:YES];
[self.view bringSubviewToFront:imageView];
you are bringing your image view to the front after increasing its frame, this is blocking the buttons actions

Position UIBarbuttonItem to the VERY left

I have a UIBarButtonItem that I want hugging the very left of the screen. I've tried putting a fixed barbutton with width=0 before the button and other configurations but there's always several pixels between the button and x=0.
I've also tried changing the imageInset of the UIButton
UIButton *moreButton = [UIButton buttonWithType:UIButtonTypeCustom];
moreButton.frame = CGRectMake(0, 0, 60, 35);
[moreButton setImage:[UIImage imageNamed:#"Morebutton"] forState:UIControlStateNormal];
moreButton.imageEdgeInsets = UIEdgeInsetsMake(0, -8, 0, 8);
[moreButton addTarget:self action:#selector(pressedMoreButton:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *moreBar = [[[UIBarButtonItem alloc] initWithCustomView:moreButton] autorelease];
But tapping near the edge of the screen doesn't trigger the selector (probably because only the image is moved but not the button itself) and increasing the size of the button doesn't affect the gap between the button and the left side.
Thanks
try to put your button to the container view and change position of button on the view.
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setBackgroundImage:buttonImage forState:UIControlStateNormal];
button.contentMode = UIViewContentModeScaleAspectFit;
button.frame= CGRectMake(-4.0, 0.0, 30, 30);
[button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
UIView *v=[[[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, cellTextSize.width+10, buttonImage.size.height)] autorelease];
v.backgroundColor = [UIColor clearColor];
[v addSubview:button];
return [[[UIBarButtonItem alloc] initWithCustomView:v] autorelease];

Dynamically change UITableViewCell's accessory view image

I have a CustomTableView Cell and it has an accessory view. How can I change the accessory view's image dynamically?
Use your custom button for accessoryview like..
// stick the pencil icon on the accessory view
UIButton* pencil = [UIButton buttonWithType:UIButtonTypeCustom];
[pencil setImage:[UIImage imageNamed:#"icon-pencil.gif"] forState:UIControlStateNormal];
pencil.frame = CGRectMake(0, 0, 15, 15);
pencil.userInteractionEnabled = YES;
[pencil addTarget:self action:#selector(didTapEditButton:) forControlEvents:UIControlEventTouchDown];
cell.accessoryView = pencil;
do not use accessoryView, just place another imageView and change accordingly
In the init method of the custom cell, you can do something like (assuming ARC btw):
UIImage *customBtnImg = [UIImage imageNamed:#"custom_btn"];
UIButton *customBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, customBtnImg.size.width, customBtnImg.size.height)];
customBtn.backgroundColor = [UIColor clearColor];
[customBtn setBackgroundImage:customBtnImg forState:UIControlStateNormal];
self.accessoryView = customBtn;
modifications to the cell except textColor and detailTextColor of cell should be done in the willDisplayCell: method and not the cellForRowAtIndexPath method ,
the solution for your question is as follows --
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cellforIndexPath:(NSIndexPath *)indexPath
{
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn setImage:[UIImage imageName:#"image.png" forControlState:UIControlStateNormal];
btn.frame = CGRectMake(0,0,28,28) ;
cell.accessoryView = btn ;
}
this code will definitely work !!

how to make button round with image background in IPhone?

I have a round button, but i will make it have a backgroud with image.
but when i do it, by setting the (what) property to an image, the button becomes rectangle because the image is a rectangle. How do I keep the button round?
Simply by doing this
#import <QuartzCore/QuartzCore.h>
myButton.layer.cornerRadius = 8;
Tested Code:
.h
-(void)vijayWithApple;
.m
-(void)vijayWithApple{
NSLog(#"vijayWithApple Called");
}
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:[UIImage imageNamed:#"Micky.png"] forState:UIControlStateNormal];
[button addTarget:self action:#selector(vijayWithApple) forControlEvents:UIControlEventTouchUpInside];
[button setTitle:#"Show View" forState:UIControlStateNormal];
button.frame = CGRectMake(135.0, 180.0, 40.0, 40.0);//width and height should be same value
button.clipsToBounds = YES;
button.layer.cornerRadius = 20;//half of the width
button.layer.borderColor=[UIColor redColor].CGColor;
button.layer.borderWidth=2.0f;
[self.view addSubview:button];
Restut
Best solution I have found especially for UITableCell. I put the code in awakeFromNib and worked right off the bat.