iOS object-c mas_make 获取viewframe 获取不到
如果你使用的是Masonry库来设置约束并且获取view的frame,那么你需要注意以下几点:
- 首先,确保你已经将Masonry库正确导入到你的项目中,并正确使用了
mas_make方法。 - 确保你已经正确设置了view的约束,包括宽度、高度、位置等。
- 在使用
mas_make方法设置约束之后,你可以使用updateConstraintsIfNeeded方法来立即更新约束,然后再获取view的frame。 - 如果你仍然无法获取到view的frame,可能是因为获取frame的时机不正确。在viewDidLoad方法中获取view的frame可能不准确,可以尝试在viewDidAppear方法中获取frame。
以下是一个示例代码,展示了如何使用Masonry设置约束并获取view的frame:
#import "ViewController.h"
#import <Masonry/Masonry.h>
@interface ViewController ()
@property (nonatomic, strong) UIView *customView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.customView = [[UIView alloc] init];
self.customView.backgroundColor = [UIColor redColor];
[self.view addSubview:self.customView];
[self.customView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.view.mas_top).offset(100);
make.left.equalTo(self.view.mas_left).offset(50);
make.width.mas_equalTo(200);
make.height.mas_equalTo(100);
}];
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[self.view layoutIfNeeded]; // 立即更新约束
CGRect frame = self.customView.frame;
NSLog(@"Custom view frame: %@", NSStringFromCGRect(frame));
}
@end
希望这可以帮助到你
原文地址: https://www.cveoy.top/t/topic/iy1N 著作权归作者所有。请勿转载和采集!