带有AutoLayout错误标题的UISegmentedControl

我有一个自定义的UITableViewCell在左侧有一个标签在右边的UISegmentedControl 。 我只是习惯于在代码中使用自动布局,这是我在我的自定义单元格(使用UIView + AutoLayout ):

 -(void)updateConstraints { self.titleLabel.translatesAutoresizingMaskIntoConstraints = NO; self.segmentedControl.translatesAutoresizingMaskIntoConstraints = NO; [self.titleLabel pinToSuperviewEdges:JRTViewPinLeftEdge inset:DB_AUTOLAYOUT_DEFAULT_INSET]; [self.titleLabel centerInContainerOnAxis:NSLayoutAttributeCenterY]; [self.segmentedControl pinToSuperviewEdges:JRTViewPinRightEdge inset:DB_AUTOLAYOUT_DEFAULT_INSET]; [self.segmentedControl pinAttribute:NSLayoutAttributeBaseline toAttribute:NSLayoutAttributeBaseline ofItem:self.titleLabel]; [super updateConstraints]; } 

我的问题是,分段控件的第一个标题总是错位:

这就是iOS 6上的样子: 在这里输入图像说明

在iOS 7上: 在这里输入图像说明

所以第一个问题是:我如何解决这个问题?

其他,切向问题:我是否通过将自动布局代码放入updateConstraints来做正确的事情,还是应该只应用一次约束?

总之来自UIView.h

如果你在-updateConstraints中设置了所有的约束条件,如果没有人约束,你甚至可能永远不会收到updateConstraints。 为了解决这个鸡和鸡蛋的问题,重写此方法返回YES。

 + (BOOL)requiresConstraintBasedLayout { return YES; }