UISegmentedControl随着自动版式标题产生位置偏移 [英] UISegmentedControl With AutoLayout Misaligns Title

查看:278
本文介绍了UISegmentedControl随着自动版式标题产生位置偏移的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义的UITableViewCell是对左侧的 UISegmentedControl 右边的标签。我刚开始习惯在code。使用自动布局,而这正是我在我的自定义单元格(使用的的UIView +自动版式):

I have a custom UITableViewCell that has a label on the left side a UISegmentedControl on the right. I'm just getting used to using Auto Layout in code, and this is what I have in my custom cell (using 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];
}

我的问题是,分段控制的第一个冠军总是错位:

My problem is that the segmented control's first title is always misaligned:

这是它看起来像在iOS 6:

This is what it looks like on iOS 6:

和iOS上的7:

And on iOS 7:

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

So first question is: How do I fix this?

其他,切的问题:我是通过把汽车布局code在 updateConstraints 做正确的事情,或者我应该只是申请的限制,一旦

Other, tangential question: Am I doing the right thing by putting auto layout code in updateConstraints or should I just apply the constraints once?

推荐答案

在短距离UIView.h

In short from UIView.h

如果你做你的约束在-updateConstraints成立,你可能从来没有收到updateConstraints如果没有人做的约束。要解决这个鸡和蛋的问题,重写这个方法返回YES。

If you do all of your constraint set up in -updateConstraints, you might never even receive updateConstraints if no one makes a constraint. To fix this chicken and egg problem, override this method to return YES.

+ (BOOL)requiresConstraintBasedLayout {
        return YES; 
}

这篇关于UISegmentedControl随着自动版式标题产生位置偏移的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆