-1

横向きと縦向きでレイアウトを変える必要があるView Controllerがあります。次のコードは、1 つの小さな点を除いてほぼ完全に機能します。移動したボタン (tag=3 のボタン) の場合、もう一方のボタン (tag=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];
    lab.text=@"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];
    }
}
4

2 に答える 2

0

ストーリーボードのView ControllerでAutoLayoutをオフにすることで問題を解決しました。また、すべてのスプリングとストラットを削除し、メイン ビューで Autoresize サブビューをオフにしました。なぜ上記のような動作になったのかはまだわかりません。しかし、まあ、今はうまくいきます。

于 2013-10-20T17:48:04.090 に答える
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 で設定された場所にとどまりました。

動作の違いは、ボタン画像を設定するためのアセット ライブラリ コードが非同期で実行されるという事実によるものだと推測しています .....

AutoLayout をオフにすると問題は解決しましたが、Popeye と同様に、その理由がわかりません (これは好きではありません)。

于 2014-01-14T14:34:52.473 に答える