如何通过编码xcode来改变按钮的位置

时间:2012-06-30 18:28:47

标签: ios xcode uibutton

我正在尝试对齐我通过代码创建的按钮,但我似乎无法更改其位置,我的代码:

- (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;
}

如果我删除“btn.center = self.center”并尝试将X和Y位置设置为

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

它甚至没有显示按钮。 如果提前感谢,请提供任何帮助!

1 个答案:

答案 0 :(得分:2)

您是否尝试过使用[btn setCenter:center];中心是CGPoint吗?