我不知道是否有人知道這是爲什麼,當你設置一個子視圖的框架viewDidLoad
和viewWillAppear
的更改不會在屏幕上影響之前正確設置框架,但如果你在viewDidAppear
他們做設置呢?無法viewDidAppear
在我來說,我加載自定義的廈門國際銀行有兩個tableviews然後試圖將它們轉移下來viewDidLoad
允許其在viewDidLoad
添加另一種觀點的空間,因爲它並不總是必要的,以顯示它。
問題是當我在viewDidLoad
或viewWillAppear
中設置幀時,它被設置在對象上,我可以通過打印出來看到它,但它不反映在屏幕上。將我設置的幀調用移動到viewDidAppear
將導致一切按預期工作。
認爲我應該可以設置框架viewDidLoad
是錯誤的嗎?當類或裝載然而沒有UI元素已經被初始化,並且因此任何試圖引用它們將被覆蓋在初始化過程,其中viewDidLoad中和viewDidAppear調用之間發生unavaliable
- (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;
}
可能是你可以顯示你的代碼? – Nekto
[UIViewController返回無效幀?]的可能重複(http://stackoverflow.com/questions/9539676/uiviewcontroller-returns-invalid-frame) –
注意!你的viewWillAppear調用'[super viewDidAppear];'它看起來像複製粘貼錯誤。 – Speakus