0

I am trying to align a button that I created through code, but I can't seem to change its position, my code:

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        self.lineWidth = DEFAULT_WIDTH;
        self.lineColor = DEFAULT_COLOR;
        UIButton* btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        btn.frame = CGRectMake(20,100, 50, 50);
        //[btn setFrame:CGRectMake(3,20, 50, 50)];
        btn.center = self.center;

        [btn setTitle:@"Clear" forState:UIControlStateNormal];
        [btn addTarget: self 
                   action: @selector(clearButtonPressed:) 
         forControlEvents: UIControlEventTouchDown];
        [self addSubview:btn];
    }
    return self;
}

If I remove the "btn.center =self.center" and try to set the X and Y position as

    "btn.frame =CGRectMake(3,20,50,50)"

it doesn't even display the button. Please any help will be appreciated , thanks in advance!

4

1 回答 1

2

Have you tried using [btn setCenter:center]; where center is a CGPoint?

于 2012-06-30T18:49:03.293 回答