我在 ViewDidload
中为所有 UI 元素设置了框架,并在
- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator
问题是,当我尝试以纵向方式从 View A --> View B
导航,然后将方向横向恢复到 View A <-- View B 时。然后框架不会相应地改变。
我尝试添加
[self.view setNeedsLayout];
[self.view layoutIfNeeded];
但是即使 viewWillTransitionToSize 在返回时没有被调用,框架仍然没有改变。
- (void)viewDidLoad {
[super viewDidLoad];
scrollView = [UIScrollView new];
scrollView.frame = CGRectMake(0,0,self.view.frame.size.width,self.view.frame.size.height-hSelecViewHeight);
scrollView.backgroundColor = [UIColor whiteColor];
scrollView.bounces = true;
scrollView.alwaysBounceVertical = true;
[self.view addSubview:scrollView];
imagePager = [[KIImagePager alloc]init];
imagePager.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.width*0.563);
imagePager.delegate = self;
imagePager.dataSource = self;
imagePager.slideshowTimeInterval = 2.0;
[imagePager setImageCounterDisabled:true];
[scrollView addSubview:imagePager];
-- and few other UI elements also --
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
self.navigationController.navigationBar.barTintColor = barColor;
[self.view setNeedsLayout];
[self.view layoutIfNeeded];
hSelView.frame = CGRectMake(0,self.view.frame.size.height-hSelecViewHeight,self.view.frame.size.width,hSelecViewHeight);
scrollView.frame = CGRectMake(0,0,self.view.frame.size.width,self.view.frame.size.height-hSelecViewHeight);
imagePager.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.width*0.563);
[hSelView reloadData];
if (categoryIndex!=0){
tableView.frame = CGRectMake(0, 0, self.view.frame.size.width, (259*[[arrListData valueForKey:@"sub_category_name"] count])+100);
[scrollView setContentSize:CGSizeMake(self.view.frame.size.width, ([[arrListData valueForKey:@"sub_category_name"] count]*259)+100)];
}else{
tableView.frame = CGRectMake(0, imagePager.frame.origin.y+imagePager.frame.size.height+1, self.view.frame.size.width, (259*[[arrList valueForKey:@"name"]count])+100);
[scrollView setContentSize:CGSizeMake(self.view.frame.size.width, ([[arrList valueForKey:@"name"]count]*259)+(self.view.frame.size.width*0.563-40)+100)];
}
}
- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator
{
[coordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> context)
{
UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
NSLog(@"orientation:- %ld",(long)orientation);
if (!isMaximized) {
content.view.frame = CGRectMake(self.view.frame.size.width-(self.view.frame.size.width/1.5)-10, self.view.frame.size.height-hSelecViewHeight-10-((self.view.frame.size.width/1.5)*0.563), self.view.frame.size.width/1.5, (self.view.frame.size.width/1.5)*0.563);
closePopButton.frame = CGRectMake(content.view.frame.size.width-40, 5, 36, 36);
hSelView.frame = CGRectMake(0,self.view.frame.size.height-hSelecViewHeight,self.view.frame.size.width,hSelecViewHeight);
scrollView.frame = CGRectMake(0,0,self.view.frame.size.width,self.view.frame.size.height-hSelecViewHeight);
imagePager.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.width*0.563);
[hSelView reloadData];
if (categoryIndex!=0){
tableView.frame = CGRectMake(0, 0, self.view.frame.size.width, (259*[[arrListData valueForKey:@"sub_category_name"] count])+100);
[scrollView setContentSize:CGSizeMake(self.view.frame.size.width, ([[arrListData valueForKey:@"sub_category_name"] count]*259)+100)];
}else{
tableView.frame = CGRectMake(0, imagePager.frame.origin.y+imagePager.frame.size.height+1, self.view.frame.size.width, (259*[[arrList valueForKey:@"name"]count])+100);
[scrollView setContentSize:CGSizeMake(self.view.frame.size.width, ([[arrList valueForKey:@"name"]count]*259)+(self.view.frame.size.width*0.563-40)+100)];
}
}
} completion:^(id<UIViewControllerTransitionCoordinatorContext> context)
{
}];
[super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
}
最佳答案
请在 viewWillAppear 方法中添加更新的代码。
- (void)viewWillAppear:(BOOL)animated
{
}
关于ios - 强制更改 subview 框架,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56196437/