UIButton可触摸区域增加但在iphone中没有反映它是否在模拟器中很好

时间:2016-08-26 06:07:30

标签: ios objective-c iphone swift

我的问题是,我想用d:/file/xyz.pdf //I want to upload this file from this location. 增加UIButton的可触摸区域。 image尺寸小于image尺寸。

我可以在UIButton中实现此目的,但不能在设备(simulator)中实现此目的。

提前致谢...

2 个答案:

答案 0 :(得分:2)

一种更简单的方法是让按钮放在您想要点击的尺寸上,然后改变该按钮内图像的尺寸。

- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation { if ([annotation isKindOfClass:[MKUserLocation class]]) return nil; if ([annotation isKindOfClass:[CustomAnnotation class]]) { CustomAnnotation *customAnnotation = (CustomAnnotation *) annotation; MKAnnotationView *annotationView = [mapView dequeueReusableAnnotationViewWithIdentifier:@"CustomAnnotation"]; if (annotationView == nil) annotationView = customAnnotation.annotationView; else annotationView.annotation = annotation; //Adding multiline subtitle code UILabel *subTitlelbl = [[UILabel alloc]init]; subTitlelbl.text = @"sri ganganagar this is my home twon.sri ganganagar this is my home twon.sri ganganagar this is my home twon. "; annotationView.detailCalloutAccessoryView = subTitlelbl; NSLayoutConstraint *width = [NSLayoutConstraint constraintWithItem:subTitlelbl attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationLessThanOrEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:150]; NSLayoutConstraint *height = [NSLayoutConstraint constraintWithItem:subTitlelbl attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationGreaterThanOrEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:0]; [subTitlelbl setNumberOfLines:0]; [subTitlelbl addConstraint:width]; [subTitlelbl addConstraint:height]; return annotationView; } else return nil; }

这将为每一方创建一个5px的“填充”。

如果您有按钮,请使用故事板。

答案 1 :(得分:2)

只需编写UIControl的扩展名,如下所示:

@interface UIControl (HitTestEdge)
@property (nonatomic, assign) UIEdgeInsets hitTestEdgeInsets;
@end


@implementation UIControl (HitTestEdge)
@dynamic hitTestEdgeInsets;

static const NSString *UIControlTouchEdgeKey = @"HitTestEdgeInsets";

-(void)setHitTestEdgeInsets:(UIEdgeInsets)hitTestEdgeInsets
{
    NSValue *value = [NSValue value:&hitTestEdgeInsets withObjCType:@encode(UIEdgeInsets)];
    objc_setAssociatedObject(self, &UIControlTouchEdgeKey, value, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

-(UIEdgeInsets)hitTestEdgeInsets
{
    NSValue *value = objc_getAssociatedObject(self, &UIControlTouchEdgeKey);
    if(value) {
        UIEdgeInsets edgeInsets; [value getValue:&edgeInsets]; return edgeInsets;
    }else {
        return UIEdgeInsetsZero;
    }
}

- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
{
    if(UIEdgeInsetsEqualToEdgeInsets(self.hitTestEdgeInsets, UIEdgeInsetsZero) ||       !self.enabled || self.hidden) {
        return [super pointInside:point withEvent:event];
    }

    CGRect relativeFrame = self.bounds;
    CGRect hitFrame = UIEdgeInsetsInsetRect(relativeFrame, self.hitTestEdgeInsets);

    return CGRectContainsPoint(hitFrame, point);
}
@end

设置hitTestEdgeInsets属性以扩展按钮单击区域。

示例:

btn.hitTestEdgeInsets = UIEdgeInsetsMake(-10, -10, -10, -10)

按钮将使用10px扩展它的点击边界。