- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在使用 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) viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<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 makeToast:orientation 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
最佳答案
这个问题似乎有点“老”了,不管怎样,至少随着 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/
我正在使用 viewWillTransitionToSize 来检测设备何时旋转到横向。根据目标大小,我可以检测是否朝向风景并根据需要调整我的特征... override func viewWillT
在我的 ios8 扩展 ActionViewController(UIViewController 的子类)中 viewWillTransitionToSize:withTransitionCoord
我想检测不同 ViewController 中的方向变化。我使用了这段代码 override func viewWillTransitionToSize(size: CGSize, withTrans
我读过帖子说 didRotate(fromInterfaceOrientation is deprecated and advising using viewWillTransitionToSize.
我的 swift 3 代码中有这个方法: func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinat
我正在开发一个针对 iOS 8.0+ 的应用程序,使用 Swift 2.2 和 Realm 1.0.2 作为我的数据存储。 我在崩溃日志中看到很多与此代码相关的崩溃,尽管我目前无法自己重现它们: //
尝试了新的旋转方法,我需要知道设备是否正在旋转,以便推迟和取消旋转时不需要的操作。 -(void)viewWillTransitionToSize:(CGSize)size withTransitio
我目前有一个名为 Page2 的 UiViewController现在,如果将 Iphone 的方向更改为横向,则会调用以下方法,这非常好 -(void) viewWillTransition
我正在为 iPad Air 2 和 iPad Mini 4 实现分屏功能。我正在尝试检测分屏大小何时发生变化。当作为主应用程序运行时,viewWillTransitionToSize 或 willTr
iOS 8 带来了两种处理屏幕旋转的新方法。 ViewWillTransitionToSize 和 willTransitionToTraitCollection。 我的问题是,如何停止这些方法中的轮
谁能解释这两种方法之间的区别? The docs for UIViewController明确声明 viewWillTransitionToSize 应该用于管理旋转,但点击进入 UIContentC
我试图通过覆盖响应 iPad 上的旋转 - (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id)c
我有一个 View Controller ,在两个嵌套 View 中有一个 View 层次结构,这些嵌套 View 放置有自动布局约束。然后我在代码中将一些 UIView 放入其中一个 View 中,
我正在使用 viewWillLayoutSubviews 来检测方向变化,但由于在 iOS 8 和 Xcode 6 中已弃用,我需要使用新的 viewWillTransitionToSize。我的问题
当我旋转设备时,我正在使用 viewWillTransitionToSize 来调整我的滑出菜单,但它似乎在 Swift 3 中被破坏了?谁能帮我解决这个问题?我的代码如下所示: func viewW
我尝试用 3 个页面创建 UIPageVC。一切都很好,但是当我旋转设备并在此更改页面之后,我将看到仅旋转页面,该页面在旋转时刻在 PageVC 中解决。 当我旋转任何页面时,如何旋转其他 2 个 V
很多人都讨论过模仿原生 iOS 相机应用程序的技术(其中 UI 元素随着设备旋转而原地旋转)。其实我之前问过一个问题here .大多数人让您锁定 UI 的方向,然后仅对您想要旋转的元素强制进行转换。这
viewWillTransitionToSize 方法在设备方向上被调用。问题是这是在所有 View Controller 上调用的。如果 View Controller A 使用 push segu
我有一个 UICollectionView,它由弹出窗口显示。 UICollectionView 包含 16 行,每行 10 个单元格,我希望这些单元格填满 UICollectionView 的宽度。
将我的 xcode 更新到 xcode9 后,我收到了一个已弃用警告列表,但我不知道如何修复它 “willRotateToInterfaceOrientation:duration:”已弃用:首先在
我是一名优秀的程序员,十分优秀!