Cocoa Touch - Custom UIButton shape

后端 未结 3 1641
无人及你
无人及你 2020-12-06 14:53

How can I make the touchable button area to be of the same shape of the image provided?
Say I have a custom button with a triangle image, how can I make sure that only t

3条回答
  •  情深已故
    2020-12-06 15:42

    Ole's OBShapedButton project should be referenced more on SO. Thanks to vikingosegundo, who's my-programming-examples github has helped in the past, you can see some of what Ole put together to take away the actions from the transparent background part of a custom UIButton. I added this to my project and was able to get it up and running with these steps.

    1. download the project
    2. add the UIImage+ColorAtPixel.h/m and OBShapedButton.h/m files to your project folder
    3. make sure the .m files are added to your target's Build Phases > Compile Sources list
    4. change the UIButton class type of all your buttons to the OBShapedButton class in IB
    5. make sure your .png file has a transparent background and make it the "Image" of the button

    This project will help anyone who has multiple overlapping UIButtons that have a transparent backgrounds, even if they are in different UIViews or completely covered by another UIButton (e.g you can't select it in IB without Editor > Arrange > Send to Back/Front). However, if you have a OBShapedButton in a UIView, where part of the transparent UIView is overlapping an OBShapedButton, you can not receive the click or drag event on the UIView covered part of the OBShapedButton, so layer your UIViews with this in mind.

    Download Ole and viking's projects for your tutorial collection......DO IT!

提交回复
热议问题