私のアプリでは、すべてのコントロールがコードで作成されています。コントロールに IB を使用したことはありません。ビューを横向きモードに回転させたいと思います。この目的のために shouldAutorotate メソッドを使用する必要があることはわかっています。
しかし、私は IB を使用したことがないので、横向きモードのコントロールのサイズを変更するにはどうすればよいですか? コードのみを使用してそれらを正しく配置するにはどうすればよいですか?
私のアプリでは、すべてのコントロールがコードで作成されています。コントロールに IB を使用したことはありません。ビューを横向きモードに回転させたいと思います。この目的のために shouldAutorotate メソッドを使用する必要があることはわかっています。
しかし、私は IB を使用したことがないので、横向きモードのコントロールのサイズを変更するにはどうすればよいですか? コードのみを使用してそれらを正しく配置するにはどうすればよいですか?
In most cases you can get views to resize themselves appropriately just by setting their autoresizingMask property to some combination of:
UIViewAutoresizingFlexibleLeftMargin
UIViewAutoresizingFlexibleRightMargin
UIViewAutoresizingFlexibleTopMargin
UIViewAutoresizingFlexibleBottomMargin
UIViewAutoresizingFlexibleWidth
UIViewAutoresizingFlexibleHeight
For example, let's say you want a view's width to increase when you rotate it to landscape, you want it to maintain the same margins relative to the top, left, and right sides of the screen, and you want its height to remain the same. This means that out of the six attributes above, only the width and bottom margin should be flexible. The other four attributes are fixed. So you would do:
yourView.autoresizingMask = UIViewAutoResizingFlexibleWidth | UIViewAutoresizingFlexibleBottomMargin;
In those rare cases when you can't get the views to behave appropriately using their autoresizingMask property, you can wrap them in a custom view and override that view's layoutSubviews
method. This method gets called when the view's frame changes due to autorotation, so you can check [[UIApplication sharedApplication] statusBarOrientation]
and update the frames of your subviews manually.
フレームを設定するなど、元の向きに配置するのと同じ方法で、コードを使用して正しく配置します。
コードのどこでこれを行うかについては、デバイスの向きが変わったときに呼び出される UIViewController のさまざまな向き変更メソッドを確認してください。たとえば、willAnimateRotationToInterfaceOrientation:duration:内でビュー内のコントロールを移動/サイズ変更できます。