2012-10-05 132 views
0

在我的应用程序的开发过程中发生了一些奇怪的事情。我有一个观点,其中有几个UIButtons。所有使用自定义艺术作品,并与CostumType UIButton。 对我来说一切都感觉很对。在模拟器和手机上。与自定义类型的UIButton似乎不能正常工作

但是,当我将应用程序放在别人的手中时,该人点击一个按钮,它将无法工作。它感觉就像按钮只是对某个水龙头的反应,实际上感觉不对(如果将其与正常行为进行比较)。

我该怎么做才能使其表现正常?正常意味着用于iOS应用的人可以像使用它一样使用它,除非它工作。

这里是一个按钮的示例代码:

focusButton = [UIButton buttonWithType:UIButtonTypeCustom]; 
    [focusButton setFrame:CGRectMake(self.bounds.size.width-(self.bounds.size.width/widthFactor)+108, self.bounds.origin.y+(self.bounds.size.height/(heightFactor*2))+2, 36, 40)]; 
    focusButton.contentHorizontalAlignment = false; // I think these lines doesn't effect the behavior 
    focusButton.contentVerticalAlignment = false; 
    [focusButton setBackgroundImage:[UIImage imageNamed:@"arrowUp.png"] forState:UIControlStateNormal]; 
    [focusButton setBackgroundImage:[UIImage imageNamed:@"arrowUpH.png"] forState:UIControlStateHighlighted]; 
    [focusButton addTarget:self action:@selector(focusOrDefocusCourse) forControlEvents:UIControlEventTouchUpInside]; 
    [self addSubview:focusButton]; 

按钮背景是这样的: Button

+0

一旦与像正常帧试验[btn_foucs SETFRAME:CGRectMake(20,20,200,40)]; –

回答

1

你可能想借此看看UIButton的财产imageEdgeInsets。这使您可以将图像仅绘制为框架的一部分,以便可触摸区域比图像本身更大(减少“丢失”按钮的机会)。你可以做到以下几点,例如:

int touch_offset = 10; 
[focusButton setFrame:CGRectMake(self.bounds.size.width-(self.bounds.size.width/widthFactor)+108-touch_offset, self.bounds.origin.y+(self.bounds.size.height/(heightFactor*2))+2-touch_offset, 36+(touch_offset*2), 40+(touch_offset*2))]; 
focusButton.imageEdgeInsets = UIEdgeInsetsMake(touch_offset, touch_offset, touch_offset, touch_offset); 

这应该使可触摸面积比两侧的图像,通过改变touch_offset值可调宽10个像素。作为一般指导原则,Apple建议使用不小于44x44像素的可触摸区域。

+0

我不确定如果我做错了什么,但即使使用imageEdgeInset属性,按钮也会变大。 – arnoapp

+0

将图片作为buttonimage使用时效果很好,而不是作为backgroudimage,这对我来说是好的。非常感谢! – arnoapp

2

这是沃金精。一旦刚好与框架测试..

UIButton *btn_foucs = [UIButton buttonWithType:UIButtonTypeCustom]; 

[btn_foucs setFrame:CGRectMake(20, 20, 200, 40)]; 

[btn_foucs setImage:[UIImage imageNamed:@"btn_erase.png"] forState:UIControlStateNormal]; 

[btn_foucs setImage:[UIImage imageNamed:@"btn_erase_h.png"] forState:UIControlStateHighlighted]; 

[self.view addSubview:btn_foucs]; 
相关问题