UIViewController は UINavigationBar の下に拡張されるのに、UITableViewController は拡張されないのはなぜですか? 質問する

UIViewController は UINavigationBar の下に拡張されるのに、UITableViewController は拡張されないのはなぜですか? 質問する

の中にがありますUITabbarControllerUINavigationControllerのサブクラスがあり、それを のとしてUIView割り当てています。これはかなり標準的なことですよね? これが私がやっている方法ですviewUIViewControllernavController

_productCategoryView = [[ProductCategoryView alloc] initWithFrame:self.view.frame];
self.view = _productCategoryView;

これviewUITableViewsubView

_productCategoryTableView = [[UITableView alloc] initWithFrame:self.frame style:UITableViewStylePlain];
_productCategoryTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
_productCategoryTableView.backgroundColor = [UIColor clearColor];
[self addSubview:_productCategoryTableView];

デバッグのためにself.backgroundColor = [UIColor blueColor]ビューを設定しています。

上記の の初期化から、tableViewビューの とテーブルの はframe同じであると思われるかもしれません。しかし、 を実行するとiOS 7、ビューの原点は の後ろに設定されます。のサブクラスでUINavigationBar設定しているため、これは理解できます。しかし、理解できないのは、テーブルが の真下に配置されている理由です。テーブルも の後ろにあるから開始するべきではないでしょうか。下のスクリーンショットを参照してください。背後の青い色合いに注目してください。self.navigationBar.translucent = YES;UINavigationControllernavBar(0, 0)navBarScenario 1navBar

シナリオ1

さて、ナビゲーションスタックにpush別のものを追加するには、 を使用します。ここでも、を含むカスタムがあります。ただし、このテーブルの上に もあり、デバッグのために も を付けました。今回は、ラベルをビューのラベルとほぼ同じに設定しています。viewController[self.navigationController pushViewController.....]UIViewtableViewUILabelredColororigin

CGRect boundsInset = UIEdgeInsetsInsetRect(self.bounds, UIEdgeInsetsMake(10, 10, 10, 10));

CGSize textSize = [_titleLabel.text sizeWithFont:_titleLabel.font
                               constrainedToSize:CGSizeMake(boundsInset.size.width, MAXFLOAT)
                                   lineBreakMode:NSLineBreakByWordWrapping];
printSize(textSize);
_titleLabel.frame = CGRectMake(boundsInset.origin.x,
                               boundsInset.origin.y,
                               boundsInset.size.width,
                               textSize.height);

上記のロジックに従うと、ラベルは表示されるはずですよね? しかし、今回は表示されません。今回はラベルが の後ろにありますnavBar

シナリオ - 2

navBar の背後の赤い色合いに注目してください。

私はサブビューをナビゲーションバーの下に一貫して配置したいと思っています。私の質問は次のとおりです。

1. How is the tableView offset by 64pixels (height of nav + status bar in iOS 7) automatically, even though it's frame is same as the view's?

2. Why does that not happen in the second view?

ベストアンサー1

デフォルトでは、iOS7 では UITableViewController のビューはナビゲーション バー/ステータス バーの下から開始されないように自動的にインセットされます。これは、Interface Builder の UITableViewController の Attributes Inspector タブの「スクロール ビューのインセットを調整する」設定、またはsetAutomaticallyAdjustsScrollViewInsets:UIViewController のメソッドによって制御されます。

UIViewController のコンテンツについて、ビューのコンテンツを上部/下部のバーの下に拡張したくない場合は、Interface Builder の「上部バーの下/下部バーの下のエッジを拡張」設定を使用できます。これは、 プロパティからアクセスできますedgesForExtendedLayout

おすすめ記事