-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];
    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

我有一个类似的问题。

这段代码:

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 解决了这个问题,但是像大力水手一样,我不明白为什么(我不喜欢)。

于 2014-01-14T14:34:52.473 回答
0

我通过在情节提要的视图控制器中关闭 AutoLayout 解决了这个问题。我还移除了所有弹簧和支柱,并在主视图中关闭了 Autoresize 子视图。我仍然不确定为什么行为如上所述。但是,嘿,它现在可以工作了。

于 2013-10-20T17:48:04.090 回答