如何获得UISearchBar基线位置?

时间:2015-11-09 07:49:29

标签: ios objective-c autolayout uilabel nslayoutconstraint

我是UISearchBar的子类,我想添加标签,它的基线将与UISearchTextField的基线对齐。我从搜索栏中提取UISearchTextField并尝试设置这样的约束:

- (void)addConstraintToBottomLabel {
    NSLayoutConstraint *leading = [NSLayoutConstraint constraintWithItem:self.bottomLabel attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeLeading multiplier:1.0 constant:0];
    NSLayoutConstraint *trailing = [NSLayoutConstraint constraintWithItem:self.bottomLabel attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationGreaterThanOrEqual toItem:self attribute:NSLayoutAttributeTrailing multiplier:1.0 constant:0];
    NSLayoutConstraint *baseline = [NSLayoutConstraint constraintWithItem:self.bottomLabel attribute:NSLayoutAttributeBaseline relatedBy:NSLayoutRelationEqual toItem:self.searchTextField attribute:NSLayoutAttributeBaseline multiplier:1.0 constant:0];
    [self addConstraints:@[leading, trailing, baseline]];
}

结果如下:

enter image description here

如您所见,标签没有对齐。

对于在约束条件下进行测试,在其他情况下效果很好,我将bottomLabel的基线与textField的底部对齐,并且效果很好。那么问题出在哪里呢?

0 个答案:

没有答案