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

标签 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)"

它甚至不显示按钮。 请提供任何帮助,在此先感谢!

最佳答案

你试过使用[btn setCenter:center];哪里的中心是 CGPoint?

关于ios - 如何通过编码xcode改变按钮的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11276590/

相关文章:

iphone - 如何从 XCode 获取同一个应用程序的第二个版本?

ios - 无法从处理程序访问类中存在的变量

iphone - 自动将生成的源文件添加到 xcode 项目

xcode - 如何在 Xcode14 中禁用粘性 header

iOS UITableView : clear section button

ios - Facebook iOS SDK 的 openActiveSession 后访问 token 过期

xcode - 为什么我的 Xcode 项目中有一些蓝色文件夹?

ios - 如何管理被禁用/启用的按钮

ios - 在 Swift 3.0 中使用 IBDesignable 在左侧和右侧带有图像的 UIButton

ios - 仅在测试目标中使用自定义字体?