2013-06-22 34 views
28

我所做的UIButton編程如何增加UIButton的選擇區域?

togglebutton = [UIButton buttonWithType:UIButtonTypeCustom]; 
togglebutton.frame = CGRectMake(42, 15, 80, 21); 
[togglebutton addTarget:self action:@selector(toggleview) 
forControlEvents:UIControlEventTouchUpInside]; 
[togglebutton setImage:[UIImage imageNamed:@"squ.png"] forState:UIControlStateNormal]; 
[buttonView addSubview:togglebutton]; 

enter image description here

它看作是在上圖中右邊的按鈕。現在的要求是這個按鈕的選擇區域應該比uibutton圖片多。這樣用戶可以通過觸摸特定按鈕的區域輕鬆地點擊按鈕。

[togglebutton setImageEdgeInsets: UIEdgeInsetsMake(0, -30, 0, -25)]; 

我試圖設置image inset但它使image irregular。請看這個問題。

+0

您是如何選擇這些插頁以及您嘗試了其他值的? – Wain

+0

看看這個,試着改變按鈕圖像的視圖模式,那麼它應該與插圖的工作和形象應該保持不變 - http://developer.apple.com/library/ios/#documentation/WindowsViews/Conceptual /ViewPG_iPhoneOS/WindowsandViews/WindowsandViews.html –

+0

@Wain我設定按鈕,圖像尺寸的 –

回答

44

您可以通過設置按鈕的contentEdgeInsets,例如實現這一目標:

toggleButton.contentEdgeInsets = UIEdgeInsetsMake(0, 15, 0, 0); //set as you require 
+6

根據財產申報,就需要負值的插圖,以增加選擇區域。 使用此屬性來調整和重新定位按鈕內容的有效繪製矩形。內容包括按鈕圖像和按鈕標題。您可以爲四個插圖(頂部,左側,底部,右側)中的每一個指定不同的值。正值縮小或縮小,使邊緣靠近按鈕的中心。負值會擴大或突破該邊緣。 – kevinl

+0

您也可以直接在xcode gui的故事板中設置它。爲此,請選擇所需按鈕並選擇「身份檢查器」,然後添加名爲「Rect」類型的「contentEdigeInsets」的「用戶定義運行時屬性」。這比在代碼中更方便。 –

+1

供參考:正值會增加按鈕的可點擊面積。 – duncanc4

-2

我會圍繞該按鈕的UIView,這樣就可以設置爲任何你想要的大小。然後我了UIView的背景色設置爲清晰的彩色(不設置不透明度爲0或手勢將不被認可)

然後添加一個UIGestureRecognizer到UIView的處理同樣IBAction爲

中當然,很多人會鼓勵/喜歡使用edgeInsets

+1

這絕對不是解決這個問題的正確方法。 – cmyr

-2

的下面是增加自定義的UIButton可觸區域的一個例子:

UIImage *ButtonImage=[UIImage imageNamed:@"myimage.png"]; 
UIButton *myButton=[UIButton buttonWithType:UIButtonTypeCustom]; 
myButton.frame=CGRectMake(10, 25, ButtonImage.size.width/2+10, ButtonImage.size.height/2+10); 
[myButton setImage:ButtonImage forState:UIControlStateNormal]; 
myButton.imageEdgeInsets = UIEdgeInsetsMake(5, 5, 5, 5); 
[myButton addTarget:self action:@selector(crossButtonClick:) forControlEvents:UIControlEventTouchDown]; 
[self.view addSubview:myButton]; 

希望這將是有益的。

0

在我的情況下,我的按鈕圖像很小,我想增加按鈕區域,而不會增加圖像大小,因爲它看起來會模糊不清。所以我增加了按鈕框架,然後設置按鈕圖像setImageEdgeInsetsedgeInsets的正值將縮小,負值將擴大。

self.menuButton = [UIButton buttonWithType:UIButtonTypeCustom]; 
[self.menuButton addTarget:self 
        action:@selector(onSideBarButtonTapped:) 
     forControlEvents:UIControlEventTouchDown]; 
[self.menuButton setImage:[UIImage imageNamed:@"menu"] 
         forState:UIControlStateNormal]; 
self.menuButton.frame = CGRectMake(10, 3, 40, 40); 

現在我將縮小圖像大小,以便按鈕不會看起來模糊。

[self.menuButton setImageEdgeInsets:UIEdgeInsetsMake(10, 10, 10, 10)]; 
2

下面的代碼解決了我的problem.This是很基本的方式, 試試這個:

button.contentEdgeInsets = UIEdgeInsetsMake(15, 20, 10, 10); //Change x,y,width,height as per your requirement. 
16

對於斯威夫特

您可以覆蓋FUNC UIView類的 'pointInside' 擴大可點擊區。

override func pointInside(point: CGPoint, withEvent event: UIEvent?) -> Bool { 
    // padding : local variable of your custom UIView, CGFloat 
    // you can change clickable area dynamically by setting this value. 

    let newBound = CGRect(
     x: self.bounds.origin.x - padding, 
     y: self.bounds.origin.y - padding, 
     width: self.bounds.width + 2 * padding, 
     height: self.bounds.height + 2 * padding 
    ) 
    return CGRectContainsPoint(newBound, point) 
} 

使用這樣,

class MyButton: UIButton { 
    var padding = CGFloat(0) // default value 

    //func pointInside at here!! 
} 

當你初始化你的按鈕,設置自定義的填充。

func initButton() { 
    let button = MyButton(frame: CGRect(x: 100, y: 100, width: 30, height: 30)) 
    button.padding = 10 // any value you want 
    view.addSubview(button) 
} 

那麼你的按鈕處於幀(X:100,Y:100,寬度:30,高度:30)

和您的按鈕的點擊區域可以是與(X幀:90, Y:90,寬度:50,高度:50)

**一定要檢查與任何其他視圖重疊,因爲其可點擊區域是比看起來大。

更新斯威夫特3

override func point(inside point: CGPoint, with event: UIEvent?) -> Bool { 
    let padding = CGFloat(10) 
    let extendedBounds = bounds.insetBy(dx: -padding, dy: -padding) 
    return extendedBounds.contains(point) 
} 
+2

這是用於自動佈局的理想解決方案。 – GoldenJoe

+0

誰也爲那些需要在不損害命中尺寸 –

+0

的選擇是沒有得到調用,即使該方法'pointInside調整按鈕的圖像的理想解決方案(點:含)'是'返回TRUE; – Saif

1

對於目標C

創建這個自定義的UIButton類,並將其分配到您的按鈕。然後從viewController中設置這些屬性值。

//This is .h file 
    #import <UIKit/UIKit.h> 
     @interface CustomButton : UIButton 
     @property (nonatomic) CGFloat leftArea; 
     @property (nonatomic) CGFloat rightArea; 
     @property (nonatomic) CGFloat topArea; 
     @property (nonatomic) CGFloat bottomArea; 
     @end 



     //.m file is following 
     #import "CustomButton.h 
     @implementation CustomButton 

     -(BOOL) pointInside:(CGPoint)point withEvent:(UIEvent *)event 
     { 
      CGRect newArea = CGRectMake(self.bounds.origin.x - _leftArea, self.bounds.origin.y - _topArea, self.bounds.size.width + _leftArea + _rightArea, self.bounds.size.height + _bottomArea + _topArea); 

      return CGRectContainsPoint(newArea, point); 
     } 
     @end 
2

在xcode的9可以執行以下操作:

xcode 9 screenshot

這意味着你有補償圖像偏移和內容偏移,以確保一個居中的圖像的定位。