Ios UINavigationController中的ViewController未填充视图
我没有找到如何使我的视图作为Ios UINavigationController中的ViewController未填充视图,ios,uinavigationcontroller,autolayout,Ios,Uinavigationcontroller,Autolayout,我没有找到如何使我的视图作为UINavigationController中的根视图控制器(或推式视图控制器)填充可用空间。我试图在这里使用自动布局,但我怀疑这是问题所在。在我的应用程序代理中 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { ViewController *viewController = [[View
UINavigationController
中的根视图控制器(或推式视图控制器)填充可用空间。我试图在这里使用自动布局,但我怀疑这是问题所在。在我的应用程序代理中
- (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]];
}
标签在屏幕上居中,而不是绑定到左上角,因为我相信视图只是在屏幕上居中,而不是填充可用空间 尝试删除以下行:
self.view.translatesAutoresizingMaskIntoConstraints = NO;
这将使视图控制器视图根据其具有的默认自动调整大小遮罩具有其全宽和全高。这是我得到的(我将视图的背景色设置为绿色):
如果仍然添加了上面的行,这就是我得到的结果(这似乎意味着视图在任何地方都找不到,因为没有绿色的痕迹):
那么,您的问题是关于视图控制器填充视图还是关于标签的布局?我“认为”它们是相关的。我希望标签位于左上角,但似乎无论我做什么,所有东西都始终居中。因此,如果它所在的视图是屏幕上可用空间的大小,那么标签将正确定位代码>?因此自动布局将起作用。否则屏幕上不会显示任何内容。我认为您可以删除self.view.translates自动调整大小gmaskintoConstraints这一行。此外,对于标签,如果要限制标签的最大宽度,您需要设置高度和宽度约束,以及preferredMaxLayoutWidth。谢谢,这正是我的行为,也是我想要的行为。