Remove IBAction UIButton Border - uibutton

I am very new to XCode and objective-c and need some help with removing the border from a UIButton.
my code is
- (IBAction)A0:(UIButton *)sender
{
UIButton *enableButton = (UIButton *)sender;
enableButton.enabled = NO;
}
I am trying to remove the border so its a clear button.

remove button border:
enableButton.buttonType = UIButtonTypeCustom;

You can hide border with following code
for this you have to import quartzcore.framework
button.layer.borderWidth = 0.0;

Related

Objective C > UITableViewCell button not depressing

I have the following button being created in the cellForRowAtIndexPath section for this tableView:
UIButton *sched_button_a1 = [UIButton buttonWithType:UIButtonTypeCustom];
sched_button_a1.frame = CGRectMake(0, 0, 110, 52);
CGRect frame_a1 = sched_button_a1.frame;
// Resize height based on length of appointment
frame_a1.size.height = heightCalc;
sched_button_a1.frame = frame_a1;
[sched_button_a1 addTarget:self action:#selector(showAppointment:) forControlEvents:UIControlEventTouchUpInside];
[sched_button_a1 setTitle:[NSString stringWithFormat:#"APPT: %#", APPT_ID ] forState:UIControlStateNormal];
sched_button_a1.layer.backgroundColor = [UIColor redColor].CGColor;
[sched_a1 addSubview:sched_button_a1];
As you can see it's calling showAppointment which is presently just:
-(void)showAppointment:(UIButton*)sender {
NSLog(#"Button pushed");
}
showAppointment is also defined here:
-(void)showAppointment:(UIButton*)sender;
The button shows up fine and gets the correct text and background and appears to be in the foreground, however, when I click on it nothing happens and it doesn't look like the button even gets depressed.
What am I doing wrong or missing?
Edit: I have gone back and removed the sched_a1 view so that the button is directly created in the contentView and this had no effect. It still appears that the only gesture being recognized is the one used to scroll the table. Tapping the button does not appear to change the color of the text or otherwise indicate that the button has been pressed and no log entry is created.
Edit 2: Added cellForRowAtIndexPath to paste bin
http://pastebin.com/WXezfW96
I had a similar issue recently, which I struggled to understand. The solution I came up with was to set the bounds of the button as well and also check for UIControlEventTouchDown rather than UIControlEventTouchUpInside.
Have you tried using cancelsTouchesInView? If you added tap gesture, I think the tap gesture or the tap in table cell overrides the touches in your button. Try using this code:
UIGestureRecognizer *tap= [[UITapGestureRecognizer alloc]initWithTarget:self action:#selector(yourAction)];
tap.cancelsTouchesInView = NO;
tap.delegate = self;
[yourButton addGestureRecognizer:tap];
But don't forget to add <UIGestureRecognizerDelegate> in your .h file
But I agree on Mike. Make it short and simple when creating table cell. I think you can use custom cell subclass and xib file.
I'm also new to objective C so if I say something wrong I'm sorry, I'm just trying to help.
UIButton *sched_button_a1 = [UIButton buttonWithType:UIButtonTypeCustom];
in this change UIButtonTypeCustom to UIButtonTypeRoundedRect
Scott, you may want to try my famous troubleshooting technique:
Determine whether the button or the action is the root cause:
UIImage *image = [UIImage imageNamed:#"image.png"];
[button setBackgroundImage:image forState:UIControlStateHighlighted|UIControlStateSelected]
Instead of:
-(void)showAppointment:(UIButton*)sender {
try
-(void)showAppointment:(id*)sender {
Use breakpoints.
If none of the above works consider refactoring your code that looks a bit messy.
Hey i have checked the code you wrote, it is working fine. Can you check if you have written similar to the code below in your cellForRowAtIndexPath.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *Identifier = #"Identifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:Identifier];
if(cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:Identifier];
}
UIButton *sched_button_a1 = [UIButton buttonWithType:UIButtonTypeCustom];
sched_button_a1.frame = CGRectMake(0, 0, 110, 52);
CGRect frame_a1 = sched_button_a1.frame;
[sched_button_a1 addTarget:self action:#selector(showAppointment:) forControlEvents:UIControlEventTouchUpInside];
[sched_button_a1 setTitle:#"Your Text" forState:UIControlStateNormal];
sched_button_a1.layer.backgroundColor = [UIColor redColor].CGColor;
[cell.contentView addSubview:sched_button_a1];
return cell;
}
I was not able to debug further as you have not kept your entire cellForRowAtIndexPath code. Hope this could help you to solve your problem
Since you are using a button type of UIButtonTypeCustom, you will need to set the attributes for UIControlStateHighlighted and UIControlStateSelected so that you achieve the effect you want for appearing to press the button.
You can change the title color or the background image for the highlighted and selected states using one of the following UIButton methods:
- (void)setTitleColor:(UIColor *)color forState:(UIControlState)state
- (void)setBackgroundImage:(UIImage *)image forState:(UIControlState)state
As an additional note regarding the code in the cellForRowAtIndexPath, you shouldn't be getting the data from sql in the method it would be destructive to your table view performance, get all the data you need before and have them stored in object in an array and then in the cellForRowAtIndexPath method you would just update the values labels from the object for the row.

UITableViewCell content overlaps delete button when in editing mode in iOS7

I am creating a UITableView with custom UITableViewCells. iOS 7's new delete button is causing some problems with the layout of my cell.
If I use the "Edit" button, which makes the red circles appear I get the problem, but if I swipe a single cell it looks perfect.
This is when the Edit button is used:
[self.tableView setEditing:!self.tableView.editing animated:YES];
This is when I swipe a single cell:
As you can se my labels overlaps the delete button in the first example. Why does it do this and how can I fix it?
try using the accessoryView and editingAccessoryView properties of your UITableViewCell, instead of adding the view yourself.
If you want the same indicator displayed in both editing and none-editing mode, try setting both view properties to point at the same view in your uiTableViewCell like:
self.accessoryView = self.imgPushEnabled;
self.editingAccessoryView = self.imgPushEnabled;
There seems to be a glitch in the table editing animation in IOS7, giving an overlap of the delete button and the accessoryView when switching back to non-editing state. This seems to happen when the accesoryView is specified and the editingAccessoryView is nil.
A workaround for this glitch, seems to be specifying an invisible editingAccessoryView like:
self.editingAccessoryView =[[UIView alloc] init];
self.editingAccessoryView.backgroundColor = [UIColor clearColor];
The problem is that in edit mode the cell's contentView changes in size. So either you have to override layoutSubviews in your cell and support the different frame sizes
- (void) layoutSubviews
{
[super layoutSubviews];
CGRect contentFrame = self.contentView.frame;
// adjust to the contentView frame
...
}
or you take the bait and switch to autolayout.
First I thought setting contentView.clipsToBounds to YES could be an ugly workaround but that does not seem to work.
I've resolved this problem with set up constraints without width only leading and trailing
As tcurdt mentioned, you could switch to autolayout to solve this issue. But, if you (understandably) don't want to mess with autolayout just for this one instance, you can set the autoresizingMask and have that turned automatically into the appropriate autolayout constraints.
label.autoresizingMask = UIViewAutoresizingFlexibleWidth;
Just use this method in your custom TableViewCell class you can get the perfect answer,
Here self is UITableviewCell
- (void)layoutSubviews {
[super layoutSubviews];
for (UIView *subview in self.subviews) {
for (UIView *subview2 in subview.subviews) {
if ([NSStringFromClass([subview2 class]) isEqualToString:#"UITableViewCellDeleteConfirmationView"]) { // move delete confirmation view
[subview bringSubviewToFront:subview2];
}
}
}
}
And if any one want to adjust the Delete Button Size, Use the following Code
- (void)layoutSubviews {
[super layoutSubviews];
for (UIView *subview in self.subviews) {
for (UIView *subview2 in subview.subviews) {
if ([NSStringFromClass([subview2 class]) isEqualToString:#"UITableViewCellDeleteConfirmationView"]) { // move delete confirmation view
CGRect rect = subview2.frame;
rect.size.height = 47; //adjusting the view height
subview2.frame = rect;
for (UIButton *btn in [subview2 subviews]) {
if ([NSStringFromClass([btn class]) isEqualToString:#"UITableViewCellDeleteConfirmationButton"]) { // adjusting the Button height
rect = btn.frame;
rect.size.height = CGRectGetHeight(subview2.frame);
btn.frame = rect;
break;
}
}
[subview bringSubviewToFront:subview2];
}
}
}
}
Best way to remove this problem is that add an image in cell and set it in Backside.
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"bgImg.png"]];
imageView.frame = CGRectMake(0, 0, 320, yourCustomCell.frame.size.height);
[yourCustomCell addSubview:imageView];
[yourCustomCell sendSubviewToBack:imageView];
If your text would overlap the delete button then implement Autolayout. It'll manage it in better way.
One more case can be generate that is cellSelectionStyle would highlight with default color. You can set highlight color as follows
yourCustomCell.selectionStyle = UITableViewCellSelectionStyleNone;
Set your table cell's selection style to UITableViewCellSelectionStyleNone. This will remove the blue background highlighting or other. Then, to make the text label or contentview highlighting work the way you want, use this method in yourCustomCell.m class.
- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated
{
if (highlighted)
self.contentView.backgroundColor = [UIColor greenColor];
else
self.contentView.backgroundColor = [UIColor clearColor];
}
I hope you understand it in a better way.
Bringing to front UITableViewCellDeleteConfirmationView in the layoutSubviews of the custom cell works for me on iPhone, but not on iPad.
I have a UITableView in the master part of a splitViewController for the iPad, and in this case
the frame of the UITableViewCellDeleteConfirmationView is (768 0; 89 44), instead of (320 0; 89 44)
So I resize the frame in the layoutSubviews method and this works for me
- (void)layoutSubviews {
[super layoutSubviews];
for (UIView *subview in self.subviews)
{
for (UIView *subview2 in subview.subviews)
{
if ([NSStringFromClass([subview2 class]) isEqualToString:#"UITableViewCellDeleteConfirmationView"])
{
CGRect frame = subview2.frame;
frame.origin.x = 320;
subview2.frame = frame;
[subview bringSubviewToFront:subview2];
}
}
}
}
If you are putting content in the UITableViewCell's contentView, be sure you use self.contentView.frame.size.width and not self.frame.size.width in layoutSubviews.
self.frame expands width in editing mode, and will cause any content on the right to extend past the bounds of the contentView. self.contentView.frame stays at the correct width (and is what you should be using).
Try this: Might be you are setting cell setBackgroundImage in cellForRowAtIndexPath (Delegate Method). Do not set this here. Set your image in:
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath { cell.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:#"cellList.png"]]; }
Enjoy Coding.
My solution is to move whole contentView to the left when Delete button showing:
override func layoutSubviews() {
super.layoutSubviews()
if editingStyle == UITableViewCellEditingStyle.Delete {
var rect = contentView.frame
rect.origin.x = self.showingDeleteConfirmation ? -15 : 38
contentView.frame = rect
}
}

Specific action for each buttons in UIScrollView

I have an UIScrollView page . On this UIscrollView I have different images inside buttons you can see it in below code, and I want to load this images in UIWebView when I press the buttons, My question is how can I know which button are click. Would you please give me some hint for implementing this?
I'm new to objective-C
Here is my action: I want to have one action for each images, rightnow I have just one action for all images, I one to have one for each image
- (IBAction)openAction:(id)sender {
NSLog(#"test");
}
The actual button is passed to the IBAction. Setting a tag on the button is one way of doing it. If you created outlets for all your buttons in Interface Builder, you can simply do something like this:
- (IBAction)openAction:(id)sender {
UIButton *b = (UIButton *)sender;
if ([b isEqual:self.outletButton1]) {
// Do something with button 1
}
else if ([b isEqual:self.outletButton2]) {
// Do something with button 2
}
}
Don't use Same Tag for Button. Use Different Tag for different button.
For ex [btn setTag:1] .
you can use [btn setTag:i];
Dynamic value
Give each button a unique tag, in your code or in Interface Builder.
Then in your action you can do:
- (IBAction)openAction:(id)sender {
UIButton *b = (UIButton *)sender;
NSLog(#"button %d is pressed", b.tag);
}
I see that you already give your imageviews tags. Those are not buttons! You should instead create UIButtons whose content are images. See UIButton's setImage:forState:
Edit: In response to your question below, here's an example:
NSMutableArray *bArray = [NSMutableArray arrayWithCapacity:kNumImages];
NSUInteger i;
for (i = 1; i <= kNumImages; i++)
{
NSString *imageName = [NSString stringWithFormat:#"image%d.jpg", i];
UIButton *btn = [[UIButton buttonWithType:UIButtonTypeCustom] retain];
btn setImage:imageName forState:UIControlStateNormal];
btn.tag = 100+i;
[bArray addObject:btn];
}
You don't have to add UIButton over UIImageView. Instead, you can set the image for UIButton like this.
[btn setImage:[UIImage imageNamed:#"image1"] forState:UIControlStateNormal];
You can access the image in the action like this:
- (IBAction)openAction:(id)sender {
if([[btn currentImage] isEqual:[UIImage imageNamed:#"image1"]]){
}
NSLog(#"test");
}

UITableView edit mode delete button padding-right

I want to give some space between UITableView edit mode delete button and cell left side (See screenshot below).
Any idea?
(source: mixdesign.kz)
Setting indentationLevel/indentationWidth did not work for me.
What worked for me was subclassing UITableViewCell and customized -layoutSubviews:
#define LEFT_EDITING_MARGIN 42
- (void)layoutSubviews
{
[super layoutSubviews];
if (self.editing && self.contentView.frame.origin.x != 0) {
CGRect frame = self.contentView.frame;
CGFloat diff = LEFT_EDITING_MARGIN - frame.origin.x;
frame.origin.x = LEFT_EDITING_MARGIN;
frame.size.width -= diff;
self.contentView.frame = frame;
}
}
#property(nonatomic) NSInteger indentationLevel is a property define din UITableViewCell documentation. Using this you can set the indentation level of a tableview Cell. Set this property from your - (void)willTransitionToState:(UITableViewCellStateMask)state
You could implements
- (void)willTransitionToState:(UITableViewCellStateMask)state
in your UITableViewCell subclass to slightly move your subviews in order to have a larger padding.

Changing button location when pressed

I have a button that should move 2 pixels down when pressed down and jump back to the original location when the press ended. I've tried touchesBegan and touchesEnded but those don't work with a button. I've also tried exchanging the button with an IUView and calling the button's method in touchesEnded but that gave the button a weird delay before moving down. Any ideas?
Did you try using the Touch Down and Touch Up events? Try moving the button location on Touch Down and restoring it to its initial value on Touch Up (Inside and Outside).
EDIT: Here's a code example:
- (IBAction)moveButtonDown:(id)sender
{
UIButton *btn = (UIButton *)sender;
btn.frame = CGRectMake(btn.frame.origin.x, btn.frame.origin.y + 2, btn.frame.size.width, btn.frame.size.height);
}
- (IBAction)moveButtonUp:(id)sender
{
UIButton *btn = (UIButton *)sender;
btn.frame = CGRectMake(btn.frame.origin.x, btn.frame.origin.y - 2, btn.frame.size.width, btn.frame.size.height);
}
Assign moveButtonDown: to the buttons "Touch Down" event and moveButtonUp: to both its "Touch Up Inside" and "Touch Up Outside" events. Tested and working, here at least ;)
EDIT: Thanks fichek, here's a solution with CGRectOffset instead of CGRectMake:
- (IBAction)moveButtonDown:(id)sender
{
UIButton *btn = (UIButton *)sender;
btn.frame = CGRectOffset(btn.frame, 0, 2);
}
- (IBAction)moveButtonUp:(id)sender
{
UIButton *btn = (UIButton *)sender;
btn.frame = CGRectOffset(btn.frame, 0, -2);
}