UINavigationController中的ViewController没有填充视图

时间:2013-06-28 19:19:41

标签: ios uinavigationcontroller autolayout

我还没有找到如何使我的视图填充可用空间作为UINavigationController中的根视图控制器(或推送视图控制器)。我试图在这里使用AutoLayout,但我怀疑这是问题。在我的app delegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    ViewController *viewController = [[ViewController alloc] init];

    UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];
    self.window.rootViewController = navigationController;

    [self.window makeKeyAndVisible];

    return YES;
}

在我的ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.view.translatesAutoresizingMaskIntoConstraints = NO;
    // Do any additional setup after loading the view, typically from a nib.

    UILabel *companyLabel = [[UILabel alloc] init];
    companyLabel.translatesAutoresizingMaskIntoConstraints = NO;
    companyLabel.text = @"We Build It";
    companyLabel.backgroundColor = [UIColor redColor];

    [self.view addSubview:companyLabel];

    NSDictionary *views = NSDictionaryOfVariableBindings(companyLabel);

    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[companyLabel]" options:0 metrics:@{} views:views]];
    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"|[companyLabel]" options:0 metrics:@{} views:views]];
}

并且标签在屏幕中居中而不是绑定到左上角,因为我相信,视图仅在屏幕中居中而不是填充可用空间。

1 个答案:

答案 0 :(得分:8)

尝试删除以下行:

self.view.translatesAutoresizingMaskIntoConstraints = NO;

这将使视图控制器视图根据其具有的默认自动调整遮罩具有其全宽和高度。这就是我得到的(我将视图的背景颜色设置为绿色):

enter image description here

如果仍然添加上面的行,这就是我得到的(这似乎暗示在任何地方都找不到视图,因为没有绿色痕迹):

enter image description here