Search code examples
iosrubyrubymotion

Why is my button not being rounded in RubyMotion?


I have a button in a RubyMotion app, created in the viewDidLoad method of my view controller class. It looks like this:

def viewDidLoad
    @button = UIButton.buttonWithType(UIButtonTypeRoundedRect)

    @button.setTitle("Tap!", forState: UIControlStateNormal)
    @button.frame = [ [85, 250], [150, 50] ]
    @button.backgroundColor = UIColor.darkGrayColor
    @button.addTarget(self, action: :actionForTheButton, forControlEvents: UIControlEventTouchUpInside)

    self.view.addSubview(@button)
end

However, my button looks like this:

enter image description here

I'm having trouble understanding why this is happening with the button, since I thought that the buttonWithType method, containing the argument of UIButtonTypeRoundedRect made a rectangular button with rounded corners. Can anyone clarify what the problem is here?


Solution

  • It's best to stay away from UIButtonTypeRoundedRect now that it's deprecated. SOURCE: https://developer.apple.com/library/ios/documentation/uikit/reference/UIButton_Class/UIButton/UIButton.html

    You can round any UIView by setting the cornerRadius on the layer though.

    I recommend rounding your UIView layer like so:

    def viewDidLoad
        @button = UIButton.buttonWithType(UIButtonTypeSystem)
    
        @button.setTitle("Tap!", forState: UIControlStateNormal)
        @button.frame = [ [85, 250], [150, 50] ]
        @button.layer.cornerRadius = 10 # SET THE ROUNDING RADIUS HERE
        @button.backgroundColor = UIColor.darkGrayColor
        @button.addTarget(self, action: :actionForTheButton, forControlEvents: UIControlEventTouchUpInside)
    
        self.view.addSubview(@button)
    end