UIButton.layer.cornerRadius doesn't exist?

14,312

Solution 1

Make sure you

#import <QuartzCore/QuartzCore.h>
into your header or implementation file.

Solution 2

Try this,

 #import <QuartzCore/QuartzCore.h>

    Button.layer.cornerRadius = 15.0;
    [Button.layer setMasksToBounds:YES];
Share:
14,312
astro8891
Author by

astro8891

Updated on June 07, 2022

Comments

  • astro8891
    astro8891 almost 2 years

    I'm implementing a custom UIButton with minimal functionality. The .h file:

    #import <Foundation/Foundation.h>
    @interface CustomButton : UIButton {
    }
    @end
    

    I'm encountering a compilation error at line (A) in the .m file:

    - (id)initWithCoder:(NSCoder *)coder {
        if(self = [super initWithCoder:coder]) {
    
            CALayer *layer = [self layer];
            NSLog(@"layer=%@",layer);
            NSLog(@"delegate=%@",[layer delegate]);
    
    #ifdef __IPHONE_3_0
            layer.cornerRadius = 4.0f; // (A) error: request for member 'cornerRadius' in something not a structure or union
    #endif
        }
        return self;
    }
    

    If I comment out line (A), I get the following output:

    2009-10-08 17:35:06.681 MyApp[2596:4e07] layer=<CALayer: 0x3cdf520>
    2009-10-08 17:35:06.683 MyApp[2596:4e07] delegate=<CustomButton: 0x3cdaff0; baseClass = UIButton; frame = (9 212; 255 55); opaque = NO; autoresize = RM+BM; layer = <CALayer: 0x3cdf520>>
    

    According to the documentation, CALayer should have a cornerRadius property. I'm using iPhone SDK 3.1 and even added an #ifdef to confirm this.

    Can someone please show me where I've overlooked the obvious?

    Thanks

  • astro8891
    astro8891 over 14 years
    If you mean "#import "CustomButton.h", I did that at the start of the .m file.
  • Ben Gottlieb
    Ben Gottlieb over 14 years
    ugh. No, sorry, the formatting screwed things up. #import <QuartzCore/QuartzCore.h>
  • Ben Clayton
    Ben Clayton about 13 years
    Ah! Finally! I've been unable to use cornerRadius for ages as I didn't know that. Cheers.
  • jrturton
    jrturton over 10 years
    This question was correctly answered years ago, your answer adds nothing.
  • chings228
    chings228 about 10 years
    if you add an image on button , be sure you set cliptobounds = YES; button.imageview.layer.cornerradius = XX
  • Ayush Goel
    Ayush Goel almost 10 years
    Your question solved my problem, but my problem was not this question.. Still upvoting for your contribution. :)