Ios 为什么UITabBarController在ViewSize中为隐藏视图提供了错误的大小?
在iOS 8中,我将我的代码移动到依赖于Ios 为什么UITabBarController在ViewSize中为隐藏视图提供了错误的大小?,ios,uitabbarcontroller,Ios,Uitabbarcontroller,在iOS 8中,我将我的代码移动到依赖于viewwilltransitionontosize来调整我的UITabBarController下的viewcontroller。但是,当视图当前未显示时,传递给它的大小不正确 我写了一个小程序来解决这个问题。它只需创建两个VCs,给它们贴上标签,将它们放在TabBarController中,然后将大小报告给ViewWillTransitionOnSize。如果您想重新创建它,可以用以下代码替换虚拟项目AppDelegate.m 输出低于程序。我是做错了
viewwilltransitionontosize
来调整我的UITabBarController下的viewcontroller。但是,当视图当前未显示时,传递给它的大小不正确
我写了一个小程序来解决这个问题。它只需创建两个VCs,给它们贴上标签,将它们放在TabBarController中,然后将大小报告给ViewWillTransitionOnSize。如果您想重新创建它,可以用以下代码替换虚拟项目AppDelegate.m
输出低于程序。我是做错了什么,还是有一个解决办法来获得正确的尺寸
#import <UIKit/UIKit.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@end
@interface MyViewController : UIViewController
@property (strong, nonatomic) NSString * detailItem;
@end
@implementation MyViewController
- (void)viewWillTransitionToSize:(CGSize)size
withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator {
NSLog(@"%@ will transition from %@ to %@",self.detailItem, NSStringFromCGSize(self.view.frame.size), NSStringFromCGSize(size));
[super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
}
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
MyViewController * firstVC = [[MyViewController alloc] initWithNibName:nil bundle:nil];
firstVC.detailItem = @"Bookmark controller";
firstVC.tabBarItem = [[UITabBarItem alloc] initWithTabBarSystemItem: UITabBarSystemItemBookmarks tag:0];
MyViewController *secondVC = [[MyViewController alloc] initWithNibName:nil bundle:nil];
secondVC.detailItem = @"History controller";
secondVC.tabBarItem = [[UITabBarItem alloc] initWithTabBarSystemItem: UITabBarSystemItemHistory tag:1];
UITabBarController * tabBarController = [[UITabBarController alloc] initWithNibName:nil bundle:nil];
[tabBarController setViewControllers: @[firstVC, secondVC]];
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.rootViewController = tabBarController;
[self.window makeKeyAndVisible];
return YES;
}
@end
#导入
@接口AppDelegate:UIResponder
@属性(强,非原子)UIWindow*window;
@结束
@接口MyViewController:UIViewController
@属性(强,非原子)NSString*detailItem;
@结束
@MyViewController的实现
-(无效)视图大小:(CGSize)大小
withTransitionCoordinator:(id)协调员{
NSLog(@“%@将从%@转换为%@”、self.detailItem、NSStringFromCGSize(self.view.frame.size)、NSStringFromCGSize(size));
[super ViewWillTransitionSize:size with TransitionCoordinator:coordinator];
}
@结束
@实现AppDelegate
-(BOOL)应用程序:(UIApplication*)应用程序使用选项完成启动:(NSDictionary*)启动选项{
MyViewController*firstVC=[[MyViewController alloc]initWithNibName:nil bundle:nil];
firstVC.detailItem=@“书签控制器”;
firstVC.tabBarItem=[[UITabBarItem alloc]initWithTabBarSystemItem:uitabbarSystemEmbitBookMarks标记:0];
MyViewController*secondVC=[[MyViewController alloc]initWithNibName:nil bundle:nil];
secondVC.detailItem=@“历史控制器”;
secondVC.tabBarItem=[[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemHistory标记:1];
UITabBarController*tabBarController=[[UITabBarController alloc]initWithNibName:nil bundle:nil];
[tabBarController SetViewController:@[firstVC,secondVC]];
self.window=[[UIWindow alloc]initWithFrame:[[UIScreen mainScreen]bounds]];
self.window.rootViewController=tabBarController;
[self.window makeKeyAndVisible];
返回YES;
}
@结束
结果如下:
书签控制器将从{375667}转换到{667375}
历史控制器将从{375667}转换到{0,0}
书签控制器将从{667375}转换到{375667}
历史控制器将从{375667}转换到{667375}
书签控制器将从{375667}转换到{375667}
历史控制器将从{375667}转换到{667375}
书签控制器将从{375667}转换到{375667}
历史控制器将从{667375}转换到{375667}
否;坦白地说,我作弊了,并使用了以下版本的viewwilltransitionosize
(在真实的程序中,这是苹果公司的AAPLTraitOverrideViewController的一个子类,让我在iphone6上做双窗格)
它依赖于当前所选选项卡中的VC已获得正确的旧帧大小这一事实,因此当为未选择的选项卡调用viewwillTransitionOnToSize
时,它会向其同级成员提供实际大小。然后应用当前变换并获取绝对值以避免负数。注:仅当VCs是tab控制器的直接子项时,此功能才起作用
如果你(或任何人)想出一个更好的方法,让我知道
- (void)viewWillTransitionToSize:(CGSize)size
withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator {
NSLog(@"%@ will transition from %@ to %@",self.detailItem, NSStringFromCGSize(self.view.frame.size), NSStringFromCGSize(size));
CGSize realSize = size;
if (self.tabBarController) {
//workaround for inaccurate sizes being delivered to non-selected tabs
NSArray * viewControllers = self.tabBarController.viewControllers;
NSUInteger myIndex = [viewControllers indexOfObject:self];
NSUInteger selectedIndex = self.tabBarController.selectedIndex;
if (selectedIndex != myIndex && myIndex != NSNotFound && selectedIndex != NSNotFound) {
//not on screen, so use size from selected tab, transformed as indicated by coordinator
UIViewController * selectedVC = viewControllers[selectedIndex];
CGSize newSize = CGSizeApplyAffineTransform(selectedVC.view.frame.size, [coordinator targetTransform]);
realSize = CGSizeMake(fabsf(newSize.width), fabsf(newSize.height));
NSLog(@" Instead of: %@, rotating to correct size: %@ ",NSStringFromCGSize(size),NSStringFromCGSize(realSize));
}
}
[super viewWillTransitionToSize:realSize withTransitionCoordinator:coordinator];
}
-(void)视图大小:(CGSize)大小
withTransitionCoordinator:(id)协调员{
NSLog(@“%@将从%@转换为%@”、self.detailItem、NSStringFromCGSize(self.view.frame.size)、NSStringFromCGSize(size));
CGSize realSize=大小;
if(self.tabBarController){
//将不准确的尺寸传递到非选定选项卡的解决方法
NSArray*viewControllers=self.tabBarController.viewControllers;
nsuiger myIndex=[viewControllers indexOfObject:self];
nsInteger selectedIndex=self.tabBarController.selectedIndex;
if(selectedIndex!=myIndex&&myIndex!=NSNotFound&&selectedIndex!=NSNotFound){
//不在屏幕上,所以使用所选选项卡中的大小,按照协调员指示进行转换
UIViewController*selectedVC=ViewController[selectedIndex];
CGSize newSize=cgsizeApplyAffinetTransform(selectedVC.view.frame.size,[coordinator targetTransform]);
realSize=CGSizeMake(fabsf(newSize.width)、fabsf(newSize.height));
NSLog(@“代替:%@,旋转到正确的大小:%@),NSStringFromCGSize(大小),NSStringFromCGSize(realSize));
}
}
[超级视图将TransitionSize:realSize与TransitionCoordinator:coordinator];
}
我尝试使用下面的代码修复此问题。我不确定这是否100%正确,所以如果有人有一个好的解决方案,请让我们知道
override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) {
for viewController in viewControllers! {
viewController.viewWillTransitionToSize(size, withTransitionCoordinator: coordinator)
}
}
嗨@mackworth,你找到解决办法了吗?我也有同样的问题。谢谢@mackworth。我尝试使用子类UITabBarController,在那里我调用ViewWillTransitionOnToSize,在这个方法中,我遍历选项卡栏中的所有视图控制器,直接调用ViewWillTransitionOnToSize,传递正确的大小。因此,仍然存在欺骗,但现在处于更高的级别!你能发布你的代码吗?您没有发现不让uitabVC处理ViewWillTransitionOnSize调用的任何副作用吗?(为什么UITabVC没有处理这样一个显而易见的案例呢?)。我还没有发现任何副作用,但你应该在你的项目中进行测试。如果有副作用,别忘了将ViewWillTransitionOnSize调用presentedViewController。如果([self-presentedViewController]){[[self-presentedViewController]视图将转换大小:带有转换的大小