【问题标题】:Force change the subviews frame强制更改子视图框架
【发布时间】:2019-05-18 06:09:03
【问题描述】:

我在ViewDidload 中为所有 UI 元素设置了框架,并在

中更改了它们的框架
- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator

问题是当我尝试从View A --&gt; View B 纵向导航,然后将方向横向恢复到视图 A

我尝试添加

[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];
}

【问题讨论】:

    标签: ios objective-c orientation


    【解决方案1】:

    请在 viewWillAppear 方法中添加更新代码。

    - (void)viewWillAppear:(BOOL)animated
      {   
    
      }
    

    【讨论】:

    • 我做了,还是一样
    • @Codecracker 显示 viewDidLoad 和 viewWillAppear 方法代码
    • 请检查 YourView set is clipsToBounds 是否启用。如果 incase 设置为NO,请设置YES
    • @Codecracker 当你回到你的第一个 viewController 然后(void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id&lt;UIViewControllerTransitionCoordinator&gt;)coordinator 这个方法是否被调用?
    • 不,当我回到第一个视图控制器时它不会被调用
    【解决方案2】:

    viewWillAppear中调用viewWillTransitionToSize方法

    -(void)viewWillAppear:(BOOL)animated {
        [super viewWillAppear:animated];
        [self viewWillTransitionToSize:self.view.frame.size withTransitionCoordinator:[self transitionCoordinator]];
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-03
      相关资源
      最近更新 更多