复制 UIButtons 框架给出了错误的位置

一些开发先生。

我想在 UIButton 上创建另一个 UIView / UIButton 所以我得到它的框架并在另一个 UIView 的声明中使用它,但它创建如下截图(蓝色视图)。顺便说一下,按钮是在情节提要上创建的。

我使用的是 Xcode 9.1 和 iOS 11.1

- (void)viewDidLoad {

    UIView *picker = [[UIView alloc] initWithFrame:_mButton.frame];
    picker.backgroundColor=[UIColor blueColor];
    [self.view addSubview: picker];
}

截图 1 截图 2

努蒂格罗

我猜正确的帧没有在viewDidLoad. 您可以尝试创建picker一个属性并在其中调整其框架viewDidLayoutSubviews

@property (strong, nonatomic) UIView *picker;

- (void)viewDidLoad {
    [super viewDidLoad];
    self.picker = [[UIView alloc] initWithFrame:_mButton.frame];
    self.picker.backgroundColor=[UIColor blueColor];
    [self.view addSubview: self.picker];
}

- (void)viewDidLayoutSubviews {
    self.picker.frame = _mButton.frame;
    [super viewDidLayoutSubviews];
}

但是,如果您有这种可能性,当然使用故事板和自动布局可能是最好的解决方案。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章