1

ビュー、UIImageView、およびボタンをプログラムで作成しました。問題は、デバイスが縦向きから横向きに切り替わった場合に位置を更新する必要があることです。どうすればよいですか?

これは、デバイスがポートレート モードかランドスケープ モードかを確認するコードである必要があります。

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
    if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft || toInterfaceOrientation == UIInterfaceOrientationLandscapeRight)
    {


    }
    else
    {


    }
}
4

4 に答える 4

1
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft || toInterfaceOrientation == UIInterfaceOrientationLandscapeRight)
    {
//change the frame for sideways direction
btn.frame=CGRectMake(x,y,width,height);
imgView.frame=CGRectMake(x,y,width,height);
view.frame=CGRectMake(x,y,width,height);
    }
else
    {
//change the frame for up/down
btn.frame=CGRectMake(x,y,width,height);
imgView.frame=CGRectMake(x,y,width,height);
view.frame=CGRectMake(x,y,width,height);
    }
 }
于 2014-07-16T15:36:03.540 に答える
0

通常、この場合は AutoLayout を使用する必要があります。ここでそれについて読むことができます: http://www.raywenderlich.com/50317/beginning-auto-layout-tutorial-in-ios-7-part-1

特別なものが必要な場合は、使用できます

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration

また

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
于 2014-07-17T05:23:22.097 に答える
0

あなたはそれをviewWillLayoutSubviewsで行います。通常、スーパービューの境界に関してフレームを変更するだけでよい場合は、向きを変える必要はありません。

于 2015-02-20T22:28:46.920 に答える