将子视图添加到视图控制器会使表视图混乱

时间:2015-06-08 21:41:36

标签: ios objective-c iphone uitableview uikit

我的UIViewController有一个UITableView作为子视图。我想在表格视图下添加背景视图,但是当我添加它时,控制器edgesForExtendedLayout似乎搞砸了

这是正确的: enter image description here

这不正确: enter image description here

我的viewDidLoad方法如下所示。如果我不添加子视图,所有看起来都没问题。

- (void)viewDidLoad
{
    [super viewDidLoad];
    // UITableView
    self.tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    self.tableView.translatesAutoresizingMaskIntoConstraints = NO;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:TAManeuvreCellId];

    [self.view addSubview:self.tableView];


    UIView *backgroundBlurView = [[UIView alloc] init];


    // autolayout
    backgroundBlurView.translatesAutoresizingMaskIntoConstraints = NO;

    NSDictionary *views = NSDictionaryOfVariableBindings(backgroundBlurView, _tableView);

    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[_tableView]|" options:0 metrics:nil views:views]];
    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[_tableView]|" options:0 metrics:nil views:views]];

    // comment this and everything is ok
    [self.view addSubview:backgroundBlurView];
    [self.view sendSubviewToBack:backgroundBlurView];

    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[backgroundBlurView]|" options:0 metrics:nil views:views]];
    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[backgroundBlurView]|" options:0 metrics:nil views:views]];
}

1 个答案:

答案 0 :(得分:0)

我认为解决这个问题的方法是:

self.navigationController.navigationBar.translucent = FALSE;
self.edgesForExtendedLayout = UIRectEdgeNone;

请参阅:

How to prevent UINavigationBar from covering top of view in iOS 7?

iOS 7 UIImagePickerController navigationbar overlap