【发布时间】:2013-04-11 17:37:13
【问题描述】:
我正在尝试将 UILabel 添加到 UITextView 并将其置于 textView 顶部的中心。它不是居中,而是放在左侧。我怀疑这是因为标签的内在大小优先于约束尝试将其拉伸到整个视图。不知道如何解决这个问题。代码如下:
self.titleLabel = [[UILabel alloc] initWithFrame:CGRectZero];
self.titleLabel.translatesAutoresizingMaskIntoConstraints = NO;
self.titleLabel.textAlignment = NSTextAlignmentCenter;
self.titleLabel.text = @"Some text that should be centered in the textView";
[self addSubview:self.titleLabel];
NSDictionary *viewsDictionary = NSDictionaryOfVariableBindings(_titleLabel);
NSArray *hConstraint =
[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[_titleLabel]-|"
options:0 metrics:nil
views:viewsDictionary];
[self addConstraints:hConstraint];
NSArray *vConstraint =
[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-(-50)-[_titleLabel]"
options:0 metrics:nil
views:viewsDictionary];
[self addConstraints:vConstraint];
这是来自 iPad 模拟器的关于标签如何左对齐的 sn-p:
【问题讨论】:
标签: iphone ios cocoa-touch autolayout