The button click on iOS added to the cell is not valid! Enlarge button's Click area (Black Magic)

Source: Internet
Author: User

In general, the Click Effect is normal! What else did you do to it? The general attentive small partners have not encountered this situation, but it!

Of course, I'm class two! Here I mainly talk about two questions, solve problems and popularize magic.

I. Popularity issues (Button clicked on cell is not valid)

Custom a Cell,cell inside creat a button! And then debugging for half a day, what reaction is not!

The Enable of 1.button is set to Yes clickable.

1. I thought I set the interactive disable!

self.userinteractionenabled = YES;

2.button frame is out of bounds!

First you need to understand the carrier of the action's passing vector, the parent trying.

Two. Universal Magic (Enlarge button's Click area)

Don't say anything, directly on the code!!!

//

Uibutton+enlargeedge.h

Peter.zhang

//

Created by Peter.zhang on 8/28/14.

Copyright (c) Peter. All rights reserved.

//

#import <objc/runtime.h>

@interface UIButton (Enlargeedge)

-(void) Setenlargeedge: (cgfloat) size;

-(void) Setenlargeedgewithtop: (cgfloat) Top right: (cgfloat) Right bottom: (cgfloat) bottom left: (CGFloat) left;

@end

---------------

//

Uibutton+enlargeedge.h

Peter.zhang

//

Created by Peter.zhang on 8/28/14.

Copyright (c) Peter. All rights reserved.

//

#import "Uibutton+enlargeedge.h"

@implementation UIButton (Enlargeedge)

static char Topnamekey;

static char Rightnamekey;

static char Bottomnamekey;

static char Leftnamekey;

-(void) Setenlargeedge: (cgfloat) size

{

Objc_setassociatedobject (self, &topnamekey, [NSNumber numberwithfloat:size], objc_association_copy_nonatomic);

Objc_setassociatedobject (self, &rightnamekey, [NSNumber numberwithfloat:size], objc_association_copy_nonatomic );

Objc_setassociatedobject (self, &bottomnamekey, [NSNumber numberwithfloat:size], Objc_association_copy_ Nonatomic);

Objc_setassociatedobject (self, &leftnamekey, [NSNumber numberwithfloat:size], objc_association_copy_nonatomic) ;

}

-(void) Setenlargeedgewithtop: (cgfloat) Top right: (cgfloat) Right bottom: (cgfloat) bottom left: (cgfloat) left

{

Objc_setassociatedobject (self, &topnamekey, [NSNumber numberwithfloat:top], objc_association_copy_nonatomic);

Objc_setassociatedobject (self, &rightnamekey, [NSNumber numberwithfloat:right], Objc_association_copy_ Nonatomic);

Objc_setassociatedobject (self, &bottomnamekey, [NSNumber Numberwithfloat:bottom], Objc_association_copy_ Nonatomic);

Objc_setassociatedobject (self, &leftnamekey, [NSNumber Numberwithfloat:left], objc_association_copy_nonatomic) ;

}

-(CGRect) Enlargedrect

{

nsnumber* TopEdge = Objc_getassociatedobject (self, &topnamekey);

nsnumber* Rightedge = Objc_getassociatedobject (self, &rightnamekey);

nsnumber* Bottomedge = Objc_getassociatedobject (self, &bottomnamekey);

nsnumber* Leftedge = Objc_getassociatedobject (self, &leftnamekey);

if (TopEdge && rightedge && bottomedge && Leftedge)

{

Return CGRectMake (Self.bounds.origin.x-leftedge.floatvalue,

Self.bounds.origin.y-topedge.floatvalue,

Self.bounds.size.width + Leftedge.floatvalue + rightedge.floatvalue,

Self.bounds.size.height + topedge.floatvalue + bottomedge.floatvalue);

}

Else

{

return self.bounds;

}

}

-(uiview*) HitTest: (cgpoint) point withevent: (uievent*) event

{

CGRect rect = [self enlargedrect];

if (Cgrectequaltorect (rect, self.bounds))

{

return [Super Hittest:point withevent:event];

}

Return Cgrectcontainspoint (rect, point)? Self:nil;

}

@end

Drag into the project directly with the line!!!! A lot of advice

UIButton *btncancel = [UIButton buttonwithtype:uibuttontypecustom];

Btncancel.frame = CGRectMake (100, 100, 10, 10);

Btncancel.backgroundcolor = [Uicolor Redcolor];

[Btncancel setenlargeedgewithtop:10 right:10 bottom:10 left:10];

[Btncancel addtarget:self Action: @selector (Cancelbtnclick:) forcontrolevents:uicontroleventtouchupinside];

[TopView Addsubview:btncancel];

The button click on iOS added to the cell is not valid! Enlarge button's Click area (Black Magic)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.