• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

ios - 如何将 viewWillLayoutSubviews 转换为 viewWillTransitionToSize

[复制链接]
菜鸟教程小白 发表于 2022-12-13 03:00:28 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我正在使用 viewWillLayoutSubviews 来检测方向变化,但在 iOS 8 和 Xcode 6 中已弃用,我需要使用新的 viewWillTransitionToSize。我的问题是我无法使用它。

我的 viewWillLayoutSubviews 是:

- (void)viewWillLayoutSubviews{
    NSInteger maxQuantityForWidth = MAX(3, [self.barButtons count]);
    CGFloat buttonWidth = (CGRectGetWidth(self.tabBar.bounds) - (kBarButtonSeparation * (maxQuantityForWidth - 1))) / maxQuantityForWidth;
    for (NSInteger i = 0; i < [self.barButtons count]; i++) {
        [self.barButtons[i] setFrame:CGRectMake(i * ((buttonWidth + kBarButtonSeparation)), 2.0, buttonWidth, CGRectGetHeight(self.tabBar.bounds) - 2.0)];
    }
    if ([self isShowingTabBar]) {
        self.functionalityContainer.frame = CGRectMake(0.0, CGRectGetHeight(self.tabBar.bounds), CGRectGetWidth(self.view.bounds), CGRectGetHeight(self.view.bounds) - CGRectGetHeight(self.tabBar.bounds));
    } else {
        self.functionalityContainer.frame = self.view.bounds;
    }

    if (IS_IPAD){
        [self updateToolbar];
        CGRect frame = self.slidingViewController.topViewController.view.frame;
        if (LANDSCAPE) {
            frame.size.width = 700;
        } else {
            frame.size.width = 768;
        }
        [self.slidingViewController.topViewController.view setFrame:frame];
    }
  }

它是使用 ECSLidingViewController 的导航部分的一部分。

我尝试了以下方法:

- (void) viewWillTransitionToSizeCGSize)size withTransitionCoordinatorid<UIViewControllerTransitionCoordinator>)coordinator {
    [coordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> context) {
        [self updateToolbar];

    } completion:^(id<UIViewControllerTransitionCoordinatorContext> context) {
        if (IS_IPAD){
            CGRect frame = self.slidingViewController.topViewController.view.frame;

            if (LANDSCAPE) {
                NSInteger maxQuantityForWidth = MAX(3, [self.barButtons count]);
                CGFloat buttonWidth = (758 - (kBarButtonSeparation * (maxQuantityForWidth - 1))) / maxQuantityForWidth;
                for (NSInteger i = 0; i < [self.barButtons count]; i++) {
                    [self.barButtons[i] setFrame:CGRectMake(i * ((buttonWidth + kBarButtonSeparation)), 2.0, buttonWidth, CGRectGetHeight(self.tabBar.bounds) - 2.0)];
                }
                frame.size.width = 768;
            } else {
                NSInteger maxQuantityForWidth = MAX(3, [self.barButtons count]);
                CGFloat buttonWidth = (758 - (kBarButtonSeparation * (maxQuantityForWidth - 1))) / maxQuantityForWidth;
                for (NSInteger i = 0; i < [self.barButtons count]; i++) {
                    [self.barButtons[i] setFrame:CGRectMake(i * ((buttonWidth + kBarButtonSeparation)), 2.0, buttonWidth, CGRectGetHeight(self.tabBar.bounds) - 2.0)];
                }
                frame.size.width = 768;
            }
            [self.slidingViewController.topViewController.view setFrame:frame];
        }

        if([self isShowingTabBar]){
            [self showTabBar];
        }else{
            [self hideTabBar];
        }
        if (IS_IPAD && LANDSCAPE) {
            self.slidingViewController.resetStrategy = ECNone;
            [self.navigationItem setLeftBarButtonItems[] animated:YES];
            if (!self.slidingViewController.underLeftShowing) {
                [self.slidingViewController anchorTopViewTo:ECRight];
            }
        } else {
            self.slidingViewController.resetStrategy = ECTapping | ECPanning;
            [self.navigationItem setLeftBarButtonItems[self.displayMenuBarButtonItem] animated:YES];
            [self.slidingViewController resetTopView];
        }
        [self.slidingViewController.topViewController.view setNeedsDisplay];
        [self.functionalityNavigationController.view setNeedsDisplay];

        [self.view setNeedsDisplay];
    }];

    NSString *orientation = [[NSString alloc]initWithFormat"Larghezza: %f Altezza: %f", self.view.frame.size.width, self.view.frame.size.height];
    [self.view makeToastrientation duration:3.0 position"bottom"];
    [super viewWillTransitionToSize: size withTransitionCoordinator: coordinator];
}

但它不起作用,当它是横向或纵向时,它似乎不“理解”。关于如何修复它的任何想法?

*** 编辑:

这里是我使用的定义

#define LANDSCAPE UIInterfaceOrientationIsLandscape(self.interfaceOrientation)
#define LANDSCAPE_RIGHT [UIDevice currentDevice].orientation == UIDeviceOrientationLandscapeLeft
#define LANDSCAPE_LEFT [UIDevice currentDevice].orientation == UIDeviceOrientationLandscapeRight
#define PORTRAIT UIInterfaceOrientationIsPortrait(self.interfaceOrientation)
#define PORTRAIT_REVERSE [UIDevice currentDevice].orientation == UIDeviceOrientationPortraitUpsideDown



Best Answer-推荐答案


这个问题似乎有点“老”,无论如何,至少随着 iOS9 的引入(尤其是多任务处理),不建议根据设备方向对当前屏幕格式下结论 - 相反,应该评估当前屏幕尺寸。以下指示屏幕格式是纵向还是横向:

- (void) viewWillTransitionToSize : (CGSize) screenSize withTransitionCoordinator : (id<UIViewControllerTransitionCoordinator> _Nonnull) coordinator
{
    BOOL isLandscapeOrientation = (screenSize.width > screenSize.height);
    ...
    ...
}

关于ios - 如何将 viewWillLayoutSubviews 转换为 viewWillTransitionToSize,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26697368/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap