How to make transparent UIButton in UITableView Cell - iphone

When My .Xib File Is Loaded That Time UITableViewCell is Fill Up With Black Background Color,For That, I am using
cell.contentView.backGroundColor = [UIColor blackColor]
But In UITableViewCell One UIButton With "Custom" Button Type Is Generated Programatically
so the area Cover By UIButton is a Not transparent (Or Cell background - black Color is Not Display), it is a white color,,,
I Want To Make UIButton With Transparent Background
SO How I Can ?

UIButton *button;
button.backgroundColor = [UIColor clearColor];

try this one it work for me
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button addTarget:self
action:#selector(aMethod:)
forControlEvents:UIControlEventTouchDown];
[button setTitle:#"Show View" forState:UIControlStateNormal];
button.frame = CGRectMake(0.0, 5.0, 160.0, 40.0);
[button setBackgroundColor:[UIColor clearColor]];
[cell.contentView addSubview:button];

Why don't you make an image of button along with text you want to show and add this as button background with button type custom.

Related

How to add a button in UIScrollView with addSubview?

I am new to iOS development and I have a problem with UIScrollView. First of all, I've created a Scroll View in a storyboard and added
#property (retain, nonatomic) IBOutlet UIScrollView *mainScroll;
in view controller handler
In the viewDidLoad method I have the following code:
- (void)viewDidLoad
{
[super viewDidLoad];
self.mainScroll.contentSize = CGSizeMake(100.0,100.0); // Have to set a size here related to your content.
// You should set these.
self.mainScroll.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
self.mainScroll.minimumZoomScale = .5; // You will have to set some values for these
self.mainScroll.maximumZoomScale = 2.0;
self.mainScroll.zoomScale = 1;
UIButton* b = [[UIButton alloc] init];
[b setBounds:CGRectMake(.0f,0.f,100.0,100.0)];
[self.mainScroll addSubview:b];
}
But the button does not appear in the simulator. However if I add a button on storyboard in the IB, the button appears and I can scroll the view.
How can I add the button in code?
Try this code
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setTitle:#"Show View" forState:UIControlStateNormal];
button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
[self.mainScroll addSubview:button];
bounds and frame are not the same thing.
Use this code instead...
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setTitle:#"Hello" forState:UIControlStateNormal];
button.frame = CGRectMake(0, 0, 100, 44);
[self.mainScroll addSubview:button];
You need to set scroll view contents size, something like the following code.
[self.mainScroll setContentSize:CGSizeMake(width, height)];
Try this :
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setTitle:#"Hello" forState:UIControlStateNormal];
button.frame = CGRectMake(self.mainScroll.frame.origin.x + 25 ,self.mainScroll.frame.origin.y +25, 50, 44);
[self.mainScroll addSubview:button];
Best and easiest way to make a button from code is:
UIButton* yourButton = [[UIButton alloc]initWithFrame:CGRectMake(x, y, width, height)];
[yourButton setTitle:#"Your Title" forState:UIControlStateNormal];
// if you set the button's image the title won't be visible, because button's title is "under" the image. I recomend you to use this if you have a cut and dried image.
[yourButton setImage:[UIImage imageNamed:#"yourImage.png"] forState:UIControlStateNormal];
// if you set the button's background image, button's title will be visible
[yourButton setBackgroundImage:[UIImage imageNamed:#"yourBackgroundImage.png"] forState:UIControlStateNormal];
// you can add target to the button, for handle the event when you touch it.
[yourButton addTarget:self action:#selector(handleToucUpInside:) forControlEvents:UIControlEventTouchUpInside];
// then add the button
[self.view addSubview:yourButton];
- (void) handleToucUpInside:(id)sender{
// here you can do anything you want, to handle the action
}

Hit area is only on label of UIButton

I create a UIButton like this:
button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0,0,100,100);
The button works, but the hit area is only on the text, not the entire button. I've set the background of the button to be [UIColor clearColor]. What do I need to do to make it so that the entire frame of the button is the hit area?
I suppose I could change the frame of the label to equal the button, but that seems hokey. Is there a better way?
try like this,
UIButton *button=[UIButton buttonWithType:UIButtonTypeCustom];
button.frame=CGRectMake(0,0,100,100);
button.backgroundColor=[UIColor clearColor];
[button setTitle:#"Set" forState:UIControlStateNormal];
[button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
[button addTarget:self action:#selector(buttonClicked) forControlEvents: UIControlEventTouchUpInside];
[self.view addSubview:button];
The button in question was being built up with layers. What I ended up doing was ensuring the background layer had a solid background color. Simply setting the background of the button to clear did not work in this case.
//set a background color so it is clickable
[layer0 setBackgroundColor: CGCOLORA(0, 1) ];
Setting the opaque property of the UIButton to true worked for me (iOS10)

UIButton setTitleColor and setImage not working

I am using the StoryBoard feature for my iPhone project. In the storyboard, I have a view controller with a table view using prototype cells. Inside the prototype cell, I have a UIButton (rounded rectangle) hooked up to a sub-classed UIButton class. The tableview gets its data from an array, and the data is printed out in cellForRowAtIndexPath.
Inside cellForRowAtIndexPath, I conditionally modify the cell's appearance. This is when I start getting problems. I am able to successfully change the text of the button. However, if I attempt to change the image of the button, using:
[cell.myButton setImage:[UIImage imageNamed:#"mynewimage.png"] forState:UIControlStateNormal];
the image is changed, but the text disappears.
If I attempt to change the text color of the button, using:
[cell.myButton setTitleColor:[UIColor colorWithRed:0 green:0 blue:0 alpha:.77] forState:UIControlStateNormal]
Nothing happens whatsoever. The color remains the same (unless I also try to change the image, in which case it completely disappears).
I am sensing I am doing something fundamentally wrong here -- do you have any suggestions?
use
- (void)setBackgroundImage:(UIImage *)image forState:(UIControlState)state;
to change image with text left
use
btn.titleLabel.textColor = yourcolor;
to change text color
Define myButton as below and check.This may be the wrong in your code.The code is:
myButton = [UIButton buttonWithType:UIButtonTypeCustom];
This is newly editted part:
[myButton setBackgroundImage:yourimage forState:UIControlStateNormal];
[myButton setTitleColor:yourcolor forState:UIControlStateNormal];
This is the way to set different properties to a Custom Button...You also find that setBackgroudimage and setTitleColor properties here...
UIButtin *myButton = [UIButton buttonWithType:UIButtonTypeCustom];
[myButton setFrame:CGRectMake(165, 205, 65, 40)];
[myButton setTitleColor:[UIColor colorWithRed:0 green:0 blue:0 alpha:.77] forState:UIControlStateNormal];
[myButton setTitle:#"Hi....." forState:UIControlStateNormal];
[myButton addTarget:self action:#selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];
[myButton setBackgroundImage:[UIImage imageNamed:#"mynewimage.png"] forState:UIControlStateNormal];
[cell addSubview:myButton];
Thank u..
I guess what you want is to change your button background:
[cell.myButton setBackgroundImage:[UIImage imageNamed:#"mynewimage.png"] forState:UIControlStateNormal];
Your text disappears because you have set the image at button front side as button main image.
[cell.myButton setImage:[UIImage imageNamed:#"mynewimage.png"] forState:UIControlStateNormal];
You need to set the button background image so that your text would be visible as the image will be drawn at the rear side of your UIButton context.
So just replace the above line of code to :
[cell.myButton setBackgroundImage:[UIImage imageNamed:#"mynewimage.png"] forState:UIControlStateNormal];

transparent UIButton in iOS 5 sdk

What is the best way to achive having test showing up as normal color but the Rect button lines being not shown effectively making it transparent.
i have tried alpha settings but no luck as it makes the whole button AND text transparent, which is not what i want i would prefer to achieve this by UIButton rather then Image.
with image i struggled to link actions / outlets among views and found it easier to connect using cntrl button and linking with views.
Any working idea would be good
Try creating a custom button:
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(x, y, w, h);
[btn addTarget:self action:#selector(buttonPushed:) forControlEvents:UIControlEventTouchUpInside];
[btn setTitle:#"Push me!" forState:UIControlStateNormal];
btn.backgroundColor = [UIColor clearColor];
[btn setTitleColor:[UIColor colorWithRed:0.3 green:0.1 blue:0.4 alpha:1.0] forState:UIControlStateNormal];
[self.view addSubview:btn];

How to add the button in uitableviewcell in ipad

I am new in ipad development i want to add the button in uitableviewcell but it is not displaying. how to add this button in tableview cell. I am writing this code in cellforRowAtIndexPath My code is:
UIButton *cellImgButton = [[UIButton alloc]initWithFrame:CGRectMake(300, 350, 40, 40)];
cellImgButton = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *buttonImage = [UIImage imageNamed:#"remove.png"];
[cellImgButton setBackgroundImage:buttonImage forState:UIControlStateNormal];
[cellImgButton addTarget:self action:#selector(cellImgButton:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:cellImgButton];
Thanks in Advance:
The problem is in your button's frame. Try this :
UIButton *cellImgButton = [UIButton buttonWithType:UIButtonTypeCustom];
[cellImgButton setFrame:CGRectMake(0, 5, 40 , 40) ];
UIImage *buttonImage = [UIImage imageNamed:#"remove.png"];
[cellImgButton setBackgroundImage:buttonImage forState:UIControlStateNormal];
[cellImgButton addTarget:self action:#selector(cellImgButton:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:cellImgButton];
Try this code. You mentioned the 'Y axis' is 350. Please check you row height. And give the 'Y' axis related to your row height.
UIButton *cellImgButton = [UIButton buttonWithType:UIButtonTypeCustom];
[cellImgButton setFrame:CGRectMake(10, 5, 40 , 40) ];
[cellImgButton setBackgroundImage:[UIImage imageNamed:#"remove.png"] forState:UIControlStateNormal];
[cellImgButton addTarget:self action:#selector(cellImgButton:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:cellImgButton];
You can just create a prototype cell in interface builder and drag a UIButton in the cell. after that create a new UITableViewCell class and link the button.
don't forget to add a identifier to the cell
1)check Table cell height
2)Check frame size of button (means x and y axies)