ios - 强制更改 subview 框架

标签 ios objective-c orientation

我在 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/

相关文章:

objective-c - 在iOS7中,为什么选中的单元格的背景色总是灰色?

c# - 有没有类似OnOrientationChanged的方法?

android - Android 应用中的相机方向

Javascript:是否可以有一个在创建对象/字符串时自动触发的函数?

ios - ELI5 : (Reactive cocoa ) - React to a Filter returning no

ios - d2i_ECParameters 返回 Null

iOS NSMutableAttributedString 因无法识别的选择器而崩溃

ios - userAcceleration 返回的计量单位是什么?

ios - 尝试后台获取期间出现 AFNetworking 错误 53

objective-c - 使用 NSTreeController/NSOutlineView 添加子对象