2013-02-01 121 views
2

我想使UISwitch周围的触摸区域每边增大10个点。看看相关文章(UIButton: Making the hit area larger than the default hit area)中的一些建议,我尝试使用下面的方法增加UISwitch周围的框架,但它会导致整个UISwitch拉伸以填充新框架。UISwitch:使触摸区域大于默认触摸区域

slide to beginarrowwith padding

有没有更合理的方法可以做到这一点?

// Increase margin around switch based on width 
const CGFloat desiredWidth = 260.0f; // real width is 240 
const CGFloat margin = 0.5f * (desiredWidth - self.beginSwitch.frame.size.width); 

// Add margin on all four sides of the switch 
CGRect newFrame = self.beginSwitch.frame; 
newFrame.origin.x -= margin; 
newFrame.origin.y -= margin; 
newFrame.size.width += 2.0f * margin; 
newFrame.size.height += 2.0f * margin; 

self.beginSwitch.frame = newFrame; 
+0

将一个不可见的按钮放在它的顶部,当它被击中时,设置实际开关的按钮状态 – trumpetlicks

+0

也许作为最后的手段,将工作。这种方法的问题是用户不能像他们期望的那样滑动交换机。 –

+1

好点,我想我永远不会滑动开关,我总是点击他们大声笑 – trumpetlicks

回答

0

我已通过把一个不可见的按钮(与较大的尺寸)中的图像(或在您的情况下,开关)的前完成这样的技艺。这样做,您可以设置开关的状态并基于开关当前状态的反转。也可以在不可见按钮的操作代码中执行您的实际操作。

相关问题