2013-07-17 29 views
0

我一直在爲iOS編寫一個應用程序,並且正在嘗試實現橫向視圖。我試圖支持iOS 5和iOS 6,所以我禁用了自動佈局。當我旋轉到橫向時,佈局正確地展開。然後我旋轉到橫向,當我旋轉回肖像時,包含我的徽標的UIImageView延伸到我的按鈕和另一個標籤下面。在iOS中旋轉視圖會導致UIImageView高度問題

從ViewController.m:

@interface ViewController() 

- (IBAction)showMenu:(id)sender; 

@end 

@implementation ViewController 

@synthesize start, menuButton, vector_status, login_status, items, recordLength, countdown, change_name, iapi, running, timeOver, setupDone, dfm, motionmanager, locationManager, recordDataTimer, timer, testLength, expNum, sampleInterval, sessionName,geoCoder,city,country,address,dataToBeJSONed,elapsedTime,recordingRate, experiment,firstName,lastInitial,userName,useDev,passWord,session_num,managedObjectContext,dataSaver,x,y,z,mag,image ; 

// displays the correct xib based on orientation and device type - called automatically upon view controller entry 
-(void) willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration { 

    if([UIDevice currentDevice].userInterfaceIdiom==UIUserInterfaceIdiomPad) { 
     if (UIInterfaceOrientationIsLandscape(toInterfaceOrientation)) { 
      [[NSBundle mainBundle] loadNibNamed:@"ViewController~landscape_iPad" 
              owner:self 
             options:nil]; 
      [self viewDidLoad]; 
     } else { 
      [[NSBundle mainBundle] loadNibNamed:@"ViewController_iPad" 
              owner:self 
             options:nil]; 
      [self viewDidLoad]; 
     } 
    } else { 
     if (UIInterfaceOrientationIsLandscape(toInterfaceOrientation)) { 
      [[NSBundle mainBundle] loadNibNamed:@"ViewController~landscape_iPhone" 
              owner:self 
             options:nil]; 
      [self viewDidLoad]; 
     } else { 
      [[NSBundle mainBundle] loadNibNamed:@"ViewController_iPhone" 
              owner:self 
             options:nil]; 
      [self viewDidLoad]; 
     } 

    } 



} 

// pre-iOS6 rotating options 
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { 
    return YES; 
} 

// iOS6 rotating options 
- (BOOL)shouldAutorotate { 
    return YES; 
} 

// iOS6 interface orientations 
- (NSUInteger)supportedInterfaceOrientations { 
    return UIInterfaceOrientationMaskAll; 
} 


- (void)viewDidLoad { 

    [super viewDidLoad]; 
    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)]; 
    [start addGestureRecognizer:longPress]; 

    recordLength = 10; 
    countdown = 10; 

    useDev = TRUE; 

    self.navigationItem.rightBarButtonItem = menuButton; 
    iapi = [iSENSE getInstance]; 
    [iapi toggleUseDev: useDev]; 


    running = NO; 
    timeOver = NO; 
    setupDone = NO; 

    if (useDev) { 
     expNum = DEV_DEFAULT_EXP; 
    } else { 
     expNum = PROD_DEFAULT_EXP; 
    } 

    dfm = [[DataFieldManager alloc] init]; 
    //[dfm setEnabledField:YES atIndex:fACCEL_Y]; 
    motionmanager = [[CMMotionManager alloc] init]; 

    userName = @"sor"; 
    passWord = @"sor"; 
    firstName = @"No Name"; 
    lastInitial = @"Provided"; 
    [self login:@"sor" withPassword:@"sor"]; 

    if (managedObjectContext == nil) { 
     managedObjectContext = [(AppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext]; 
    } 

    // DataSaver from Data_CollectorAppDelegate 
    if (dataSaver == nil) { 
     dataSaver = [(AppDelegate *) [[UIApplication sharedApplication] delegate] dataSaver]; 
     NSLog(@"Datasaver Details: %@", dataSaver.description); 
     NSLog(@"Current count = %d", dataSaver.count); 
    } 


} 

- (void) viewDidAppear:(BOOL)animated { 
    [super viewDidAppear:animated]; 
    if (self.isMovingToParentViewController == YES) { 
     change_name = [[CODialog alloc] initWithWindow:self.view.window]; 
     change_name.title = @"Enter First Name and Last Initial"; 
     [change_name addTextFieldWithPlaceholder:@"First Name" secure:NO]; 
     [change_name addTextFieldWithPlaceholder:@"Last Initial" secure:NO]; 
     UITextField *last = [change_name textFieldAtIndex:1]; 
     [last setDelegate:self]; 
     [change_name addButtonWithTitle:@"Done" target:self selector:@selector(changeName)]; 
     [change_name showOrUpdateAnimated:YES]; 


     NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults]; 

     BOOL x1 = [prefs boolForKey:@"X"]; 
     BOOL y1 = [prefs boolForKey:@"Y"]; 
     BOOL z1 = [prefs boolForKey:@"Z"]; 
     BOOL mag1 = [prefs boolForKey:@"Magnitude"]; 


     if (x) 
      x = x1; 
     if (y) 
      y = y1; 
     if (z) 
      z = z1; 
     if (mag) 
      mag = mag1; 

     [self willRotateToInterfaceOrientation:(self.interfaceOrientation) duration:0]; 
     NSLog(@"Frog FROG FROG"); 

     /*[image setAutoresizesSubviews:YES]; 
     image.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin; 

     */ 
    } 
} 

從AppDelegate.m:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 
    // Override point for customization after application launch. 
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) { 
     self.viewController = [[ViewController alloc] initWithNibName:@"ViewController_iPhone" bundle:nil]; 
    } else { 
     self.viewController = [[ViewController alloc] initWithNibName:@"ViewController_iPad" bundle:nil]; 
    }  
    UINavigationController *navigation = [[UINavigationController alloc] initWithRootViewController:self.viewController]; 
    navigation.navigationBar.barStyle = UIBarStyleBlackOpaque; 
    self.window.rootViewController = navigation; 
    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults]; 
    [prefs setBool:YES forKey:@"Y"]; 
    self.viewController.setupDone = YES; 
    [self.window makeKeyAndVisible]; 
    return YES; 
} 
+0

檢查UIImageView的autoresizingMask屬性或通過Interface Builder。 –

+0

當我設置時,圖像完全消失。之前,它在那裏,但UIImageView被拉伸。 – balabhv

+0

我的意思是你必須取消設置錯誤的autoResizingMask設置。首先檢查UIViewAutoresizingFlexibleHeight。 –

回答

0

檢查autoResizingMask該UIImageView的財產。你應該先看看UIViewAutoresizingFlexibleHeight