これにいくつか同様の質問がありましたが、私の具体的なニーズには対応していません。私はUIViewの最大使用可能なフレームサイズを返すジェネリックヘルパーメソッドを書くことができるようにしたいと思います。アプリケーションがステータスバー、ナビゲーションバー、タブバーのいずれかの組み合わせを持っているかどうかを考慮して、時間。UIViewの使用可能な最大フレームサイズをプログラムで決定します
[UIScreen mainScreen].applicationFrame
プロパティから得たことができ
+ (CGRect) maximumUsableFrame;
またはステータスバーなしでサイズを取得するが、私は理解することはできません:
メソッドの定義はUIScreenの延長のようになりナビゲーションバーまたはタブバーが存在するかどうかを判断する方法を説明します。私は、アプリケーションのデリゲートでいくつかのグローバルフラグを維持することを考えましたが、これは本当に厄介なようであり、コードが一般的で再利用可能になるのを止めます。また、UIViewをパラメータとして渡し、ビューのウィンドウを取得してからrootViewControllerを呼び出し、ナビゲーションコントローラのプロパティが設定されているかどうかを確認しました。そうであれば、ナビゲーションコントローラが隠されているかどうかをチェックする。あなたが私に尋ねるならば、非常にclunky。
どのような考えにも感謝します。
デイブ
EDIT:
// Extension to UIViewController to return the maxiumum usable frame size for a view
@implementation UIViewController (SCLibrary)
- (CGRect) maximumUsableFrame {
static CGFloat const kNavigationBarPortraitHeight = 44;
static CGFloat const kNavigationBarLandscapeHeight = 34;
static CGFloat const kToolBarHeight = 49;
// Start with the screen size minus the status bar if present
CGRect maxFrame = [UIScreen mainScreen].applicationFrame;
// If the orientation is landscape left or landscape right then swap the width and height
if (UIInterfaceOrientationIsLandscape(self.interfaceOrientation)) {
CGFloat temp = maxFrame.size.height;
maxFrame.size.height = maxFrame.size.width;
maxFrame.size.width = temp;
}
// Take into account if there is a navigation bar present and visible (note that if the NavigationBar may
// not be visible at this stage in the view controller's lifecycle. If the NavigationBar is shown/hidden
// in the loadView then this provides an accurate result. If the NavigationBar is shown/hidden using the
// navigationController:willShowViewController: delegate method then this will not be accurate until the
// viewDidAppear method is called.
if (self.navigationController) {
if (self.navigationController.navigationBarHidden == NO) {
// Depending upon the orientation reduce the height accordingly
if (UIInterfaceOrientationIsLandscape(self.interfaceOrientation)) {
maxFrame.size.height -= kNavigationBarLandscapeHeight;
}
else {
maxFrame.size.height -= kNavigationBarPortraitHeight;
}
}
}
// Take into account if there is a toolbar present and visible
if (self.tabBarController) {
if (!self.tabBarController.view.hidden) maxFrame.size.height -= kToolBarHeight;
}
return maxFrame;
}
NavigationBarコントロールやTabBarが表示されている場合は、高さをこのように見つけることができます: 'self.navigationController.navigationBar.frame.size.height;'と 'self.tabBarController.tabBar.frame.size.height'を。なぜタブバー高さの変数がkToolBarHeightと呼ばれていますか?=) – Alexander
これをどのように使いたいかによって、少なくとも私の場合はnavControllerのフレーム原点も更新しました。そのため、ViewControllerで私のビューをどこに配置できるかがわかっていました。さもなければ、あなたはまだナビバーの下に終わることができます。 –