Tôi đã tự hỏi liệu có ai biết lý do tại sao khi bạn đặt khung của một chế độ xem phụ trong viewDidLoad
và viewWillAppear
các thay đổi không ảnh hưởng đến màn hình, nhưng nếu bạn đặt nó ở viewDidAppear
thì sao?Không thể đặt khung chính xác trước khi xemDidAppear
Trong trường hợp của tôi, tôi đang tải xib tùy chỉnh với hai lần xem bảng rồi cố gắng chuyển chúng xuống viewDidLoad
để cho phép không gian cho một chế độ xem khác được thêm vào viewDidLoad
vì không phải lúc nào cũng cần thiết để hiển thị nó.
Vấn đề là khi tôi đặt khung ở viewDidLoad
hoặc viewWillAppear
nó được đặt trên đối tượng, tôi có thể thấy bằng cách in ra, nhưng nó không được phản ánh trên màn hình. Di chuyển các cuộc gọi khung đã đặt thành viewDidAppear
sẽ khiến mọi thứ hoạt động như mong đợi.
Có sai khi nghĩ rằng tôi có thể đặt khung ở viewDidLoad
không?
- (id)init {
if (self = [super initWithNibName:@"MyView" bundle:nil]) {}
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
self.descriptionWebView = [[[UIWebView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 200)] autorelease];
[self.view addSubview:self.descriptionWebView];
self.tableView1.autoresizingMask = UIViewAutoresizingNone;
self.tableView2.autoresizingMask = UIViewAutoresizingNone;
[self.descriptionWebView loadRequest:[NSURLRequest requestWithURL:[[NSBundle mainBundle] URLForResource:@"...." withExtension:@"html"]]];
table1LocationWithHeader = CGRectMake(self.tableView1.frame.origin.x, 200, self.tableView1.frame.size.width, self.tableView1.frame.size.height - 200);
table2LocationWithHeader = CGRectMake(self.tableView2.frame.origin.x, 200, self.tableView2.frame.size.width, self.tableView2.frame.size.height - 200);
//this will NOT work
self.tableView1.frame = table1LocationWithHeader;
self.tableView2.frame = table2LocationWithHeader;
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
//this will NOT work
self.tableView1.frame = table1LocationWithHeader;
self.tableView2.frame = table2LocationWithHeader;
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
//this WILL work
self.tableView1.frame = table1LocationWithHeader;
self.tableView2.frame = table2LocationWithHeader;
}
//I added these after comments for stack overflow users, it seems like viewDidLayoutSubviews is the best place to set the frame
- (void)viewWillLayoutSubviews {
[super viewWillLayoutSubviews];
//this will NOT work
self.tableView1.frame = table1LocationWithHeader;
self.tableView2.frame = table2LocationWithHeader;
}
- (void)viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
//this WILL work
self.tableView1.frame = table1LocationWithHeader;
self.tableView2.frame = table2LocationWithHeader;
}
Bạn có thể hiển thị mã của mình không? – Nekto
có thể trùng lặp của [UIViewController trả về khung không hợp lệ?] (Http://stackoverflow.com/questions/9539676/uiviewcontroller-returns-invalid-frame) –
NB! ViewWillAppear của bạn gọi '[super viewDidAppear];' Có vẻ như lỗi sao chép-dán. – Speakus