复制 UIButtons 框架给出了错误的位置
Copying UIButtons frame gives wrong position
我想在 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];
}
Screenshot 1
Screnshot 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];
}
但当然,如果有这种可能性,使用故事板和自动布局可能是最好的解决方案。
我想在 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];
}
Screenshot 1 Screnshot 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];
}
但当然,如果有这种可能性,使用故事板和自动布局可能是最好的解决方案。