更改iphone的方向,然后将其向后更改会将我的视图减半?

更改iphone的方向,然后将其向后更改会将我的视图减半?,iphone,Iphone,我的iphone应用程序设置中有一个视图控制器,因此当我将方向从纵向更改为横向时,我会更改视图。当我将方向从横向改回纵向时,初始视图会返回,但这次它全部填充到屏幕的左侧。最终,当我改变方向足够多次时,一切都完全消失了。这是初学者经常遇到的问题吗?我可能做错了什么 在我的根控制器中,我只允许在显示特定视图时更改方向,如下所示: - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrient

我的iphone应用程序设置中有一个视图控制器,因此当我将方向从纵向更改为横向时,我会更改视图。当我将方向从横向改回纵向时,初始视图会返回,但这次它全部填充到屏幕的左侧。最终,当我改变方向足够多次时,一切都完全消失了。这是初学者经常遇到的问题吗?我可能做错了什么

在我的根控制器中,我只允许在显示特定视图时更改方向,如下所示:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
if (self.currentView == (NSInteger*)Chart || self.currentView == (NSInteger*)Detail) {
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
return (interfaceOrientation == UIInterfaceOrientationPortrait);
其中
self.currentView
是我当前拥有的视图的枚举。我希望确保细节视图保持为纵向视图,但当我在该视图上更改方向时,我希望它将视图更改为图形。同样,这在第一次使用时效果很好,但当我从图形切换回细节时,它会将细节视图上的所有控件塞满到屏幕的左侧

以下是我如何更改视图:

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration{

if (self.currentView == (NSInteger*)Detail && (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft || toInterfaceOrientation == UIInterfaceOrientationLandscapeRight)) {
    [[NSNotificationCenter defaultCenter] postNotificationName:@"changeView" object:self userInfo:[NSDictionary dictionaryWithObject:[NSNumber numberWithInt:Chart] forKey:@"view"]];
}

if (self.currentView == (NSInteger*)Chart && (toInterfaceOrientation == UIInterfaceOrientationPortrait)) {
    [[NSNotificationCenter defaultCenter] postNotificationName:@"changeView" object:self userInfo:[NSDictionary dictionaryWithObject:[NSNumber numberWithInt:Detail] forKey:@"view"]];
}

@贾斯汀,我曾经这样做,这让我陷入了和你一样的境地。也许你可以看看你是否做过这样的事情

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    CGRect rect;
    if (interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) {
        rect = CGRectMake(tableView.frame.origin.x,tableView.frame.origin.y,
                                 tableView.frame.size.width - 50, tableView.frame.size.height - 30);        
    }
    else {
        rect = CGRectMake(tableView.frame.origin.x,aBar.frame.origin.y,
                          tableView.frame.size.width, tableView.frame.size.height);     
    }
    [tableView setFrame:rect];
    return YES;
}
if (interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) {
    tableView.frame = CGRectMake(tableView.frame.origin.x,tableView.frame.origin.y,
                      tableView.frame.size.width - 50, tableView.frame.size.height - 30);       
}
else {
    tableView.frame = originalTableViewFrame;
}
我想要的只是一个在纵向模式下具有小框架的表视图,在不保存原始框架的情况下,我尝试减小其宽度和高度,最终在多次旋转后将表视图缩小到非常小的尺寸

洛兹。我应该先保存原始的tableview框架,然后执行类似的操作

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    CGRect rect;
    if (interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) {
        rect = CGRectMake(tableView.frame.origin.x,tableView.frame.origin.y,
                                 tableView.frame.size.width - 50, tableView.frame.size.height - 30);        
    }
    else {
        rect = CGRectMake(tableView.frame.origin.x,aBar.frame.origin.y,
                          tableView.frame.size.width, tableView.frame.size.height);     
    }
    [tableView setFrame:rect];
    return YES;
}
if (interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) {
    tableView.frame = CGRectMake(tableView.frame.origin.x,tableView.frame.origin.y,
                      tableView.frame.size.width - 50, tableView.frame.size.height - 30);       
}
else {
    tableView.frame = originalTableViewFrame;
}

检查您的视图和可能的父视图上是否打开了autoresizeSubviews(在XIB/Inteface Builder中),如果手动更改帧,请尝试将其关闭,这解决了我的问题

您如何处理旋转?您是否以编程方式更改了任何内容?你能发布代码吗?我添加了代码来显示我是如何通过旋转来改变视图的。在我的根控制器中,我删除了存在的视图,然后使用
[self.view addSubView:newView]
添加了新的视图。当我更改方向时,我没有对框架做任何操作,我只是在根控制器的子视图中添加和删除视图。我需要重置帧大小吗?如果我只是交换子视图中的内容,我想我不必这么做。你注意到你正在使用if(self.currentView==(NSInteger*)图表| | self.currentView==(NSInteger*)细节)where in(NSInteger*)不是必需的只是comapre self.currentView==图表或self.currentView=[图表intValue]谢谢。我是Objective C和iPhone开发人员的新手,非常欣赏这一技巧。如果要从其superview中删除某个视图,则必须在将该视图添加到任何其他视图之前再次设置其(您删除的视图)框架(它可以是您先前删除的同一个superview),请尝试使用NSLog(@“框架为%@”,NSStringFromCGRect(urView.frame));在将其添加到任何视图之前。我们正在靠近它!我试图手动设置帧,看看是否可以使其一致,但仍然是错误的。至少它一直是错误的。它第一次看起来还是对的,但当我改变方向并把它改回来时,它仍然是错的,但它不再消失。下一步我会更具体地试用你们的样品。