2013-10-20 46 views
-1

我有一個視圖控制器,需要在橫向和縱向之間進行不同的佈局。除了一件小事外,下面的代碼幾乎完美地工作。在被移動的按鈕(標籤= 3的按鈕)的情況下,當我點擊另一個按鈕(標籤= 0並調用(IBAction)myButton:)時,按鈕移動到放置位置在故事板中。標籤不移動,只是按鈕。只有在(IBAction)myButton中設置的標題值與按鈕按下前的值不同時纔會發生這種情況。爲什麼按鈕沒有按預期方式移動

- (IBAction)myButton:(UIButton *)sender { 
    UIButton *but2 = (UIButton *)[self.view viewWithTag:3]; 
    [but2 setTitle: @"A" forState:UIControlStateNormal]; 
    UILabel *lab =(UILabel *)[self.view viewWithTag:2]; 
    [email protected]"B"; 
} 
-(void)rotateViewToPortrait{ 
    UIButton *but2 = (UIButton *)[self.view viewWithTag:3]; 
    [but2 setFrame:CGRectMake(100, 500, but2.frame.size.width, but2.frame.size.height)]; 
    UILabel *lab =(UILabel *)[self.view viewWithTag:2]; 
    [lab setFrame:CGRectMake(100,550 , lab.frame.size.width, lab.frame.size.height)]; 
} 
-(void)rotateViewToLandscape{ 
    UIButton *but2 = (UIButton *)[self.view viewWithTag:3]; 
    [but2 setFrame:CGRectMake(500, 100 , but2.frame.size.width, but2.frame.size.height)]; 
    UILabel *lab =(UILabel *)[self.view viewWithTag:2]; 
    [lab setFrame:CGRectMake(500,150 , lab.frame.size.width, lab.frame.size.height)]; 
} 
-(void)viewDidAppear:(BOOL)animated{ 
    UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation; 
    if ((orientation==UIInterfaceOrientationLandscapeRight)||(orientation==UIInterfaceOrientationLandscapeLeft)) { 
     [self willAnimateRotationToInterfaceOrientation:UIInterfaceOrientationLandscapeRight duration:0]; 
    } else { 
     [self willAnimateRotationToInterfaceOrientation:UIInterfaceOrientationPortrait duration:0]; 
    } 
} 
-(void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration{ 
    if ((toInterfaceOrientation == UIInterfaceOrientationLandscapeRight)||(toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft)) { 
     [self rotateViewToLandscape]; 
    } else if ((toInterfaceOrientation == UIInterfaceOrientationPortrait)||(toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)){ 
    [self rotateViewToPortrait]; 
    } 
} 

回答

0

我解決了故事板中視圖控制器中的AutoLayout問題。我還刪除了所有彈簧和支柱,並在主視圖中關閉了Autoresize子視圖。 我仍然不確定爲什麼行爲如上所述。但嘿,它現在有效。

+0

Upvoted-這立即解決了我的問題。我有一個沒有使用任何約束的視圖,但xib選中了「使用自動佈局」,其子視圖的位置以意想不到的方式偏移。 – bmovement

0

我有類似的問題。

此代碼:

if (howManyButtons==2) { 
    button1.hidden=true; 
    button2.hidden=false; 
    [button2 setFrame:CGRectMake(177,button2.frame.origin.y,button2.frame.size.width,button2.frame.size.height)]; 
    button3.hidden=false; 
    [button3 setFrame:CGRectMake(703,button2.frame.origin.y,button2.frame.size.width,button2.frame.size.height)]; 
    button4.hidden=true; 
} else { 
    button1.hidden=false; 
    [button1 setFrame:CGRectMake(71,button2.frame.origin.y,button2.frame.size.width,button2.frame.size.height)]; 
    button2.hidden=false; 
    [button2 setFrame:CGRectMake(317,button2.frame.origin.y,button2.frame.size.width,button2.frame.size.height)]; 
    button3.hidden=false; 
    [button3 setFrame:CGRectMake(563,button2.frame.origin.y,button2.frame.size.width,button2.frame.size.height)]; 
    button4.hidden=false; 
    [button4 setFrame:CGRectMake(809,button2.frame.origin.y,button2.frame.size.width,button2.frame.size.height)]; 
} 

當(howManyButtons == 2),如果按鈕上的圖像已被裝載有這樣的代碼:如預期

UIImage *btnImage4 = [UIImage imageWithContentsOfFile:path]; 
    [button4 setImage:btnImage4 forState:UIControlStateNormal]; 

的按鈕移動,但當按鈕上的圖像已加載此代碼(從iPad的照片庫中獲取圖像而不是應用程序本身的資源時):

NSURL *testURL = [[NSUserDefaults standardUserDefaults] URLForKey:[self albumNameForPic:albumNo picNo:[self picNoForAlbum:albumNo picNo:picNo]]]; 
ALAssetsLibrary *assetsLibrary = [[ALAssetsLibrary alloc] init]; 
[assetsLibrary assetForURL:testURL resultBlock: ^(ALAsset *asset){ 

    CGImageRef imageRef = [asset thumbnail]; 

    if (imageRef) { 
     switch (buttonNo) { 
      case 1: 
       [button1 setImage:[UIImage imageWithCGImage:imageRef scale:0.5 orientation:UIImageOrientationUp] forState:UIControlStateNormal]; 
       break; 
      case 2: 
       [button2 setImage:[UIImage imageWithCGImage:imageRef scale:0.5 orientation:UIImageOrientationUp] forState:UIControlStateNormal]; 
       break; 
      case 3: 
       [button3 setImage:[UIImage imageWithCGImage:imageRef scale:0.5 orientation:UIImageOrientationUp] forState:UIControlStateNormal]; 
       break; 
      case 4: 
       [button4 setImage:[UIImage imageWithCGImage:imageRef scale:0.5 orientation:UIImageOrientationUp] forState:UIControlStateNormal]; 
       break; 
      default: 
       break; 
     } 

    } 
} failureBlock:^(NSError *error) { 
    // Handle failure. 
}]; 

按鈕沒有移動,他們留在他們被設置在Main.storyboard中的位置。

我猜的行爲不同的是下降到了資源庫代碼設置按鈕圖像是異步運行的事實.....

關閉自動版式解決了這個問題,但是,像大力水手,我不明白爲什麼(我不喜歡)。

相關問題