我正在尝试将 a 添加UILabel
到 aUITextView
并使其居中于 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 模拟器中标签如何左对齐的片段: