Ios WillAnimateRotationInterfaceOrientation在Xcode中不起作用

Ios WillAnimateRotationInterfaceOrientation在Xcode中不起作用,ios,objective-c,cocoa-touch,Ios,Objective C,Cocoa Touch,我在WillAnimateRotationInterfaceOrientation中有以下方法: -(void) willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration { if (toInterfaceOrientation == UIInterfaceOrientationPortrait) {

我在WillAnimateRotationInterfaceOrientation中有以下方法:

-(void) willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
if (toInterfaceOrientation == UIInterfaceOrientationPortrait) {
    button1.frame = CGRectMake(20, 20, 125, 125);
    button2.frame = CGRectMake(175, 20, 125, 125);
    NSLog(@"is portrait");
}else{
    button1.frame = CGRectMake(20, 20, 125, 125);
    button2.frame = CGRectMake(175, 155, 125, 125);
    NSLog(@"is landscape");
}
}

当我旋转iOS模拟器时,我可以看到日志出现,这意味着调用了这个方法。但是,按钮的帧不会更改。有人能帮我吗


我找到了一些关于更改VIEWWILLAPPEAR方法或类似方法的相关答案,但没有一个有效。

您还需要强制按钮重新绘制,这可以通过在方法底部添加两行来实现:

-(void) willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
                                         duration:(NSTimeInterval)duration
{
    if (toInterfaceOrientation == UIInterfaceOrientationPortrait) {
        button1.frame = CGRectMake(20, 20, 125, 125);
        button2.frame = CGRectMake(175, 20, 125, 125);
        NSLog(@"is portrait");
    }else{
        button1.frame = CGRectMake(20, 20, 125, 125);
        button2.frame = CGRectMake(175, 155, 125, 125);
        NSLog(@"is landscape");
    }
    [button1 setNeedsDisplay];
    [button2 setNeedsDisplay];
}

在该方法中设置Xcode断点,并查看button1是有效对象还是为NULL。您可以改为添加一行,如ifbutton1==NULL NSLog@why is button1 NULL;在你的方法中,这也会告诉你。@MichaelDautermann我刚刚检查过它,它是一个有效的对象。我用故事板中的按钮链接IBOutlet。我没有做任何花哨的事。所以我认为问题不在于对象,而在于方法。方法从Xcode4到Xcode5变化很大。我现在正在关注NewBoston的YouTube教程,我想他正在使用Xcode4。所以问题可能是这个方法不知怎么被苹果更新了。谢谢你的回答,但它不起作用。不过,我认为你正朝着正确的方向前进。