iOS 7上的自定义布局将元素渲染为高

时间:2013-09-20 12:38:29

标签: layout view ios7

iOS 7上的自定义布局存在问题。一切都显得太高,即在导航栏下方。在iOS 6上,即使使用iOS 7 SDK进行编译,它也像魅力一样。

下面我发布用于初始化和布局视图的代码。我不使用Interface Builder,我不愿意。任何帮助表示赞赏。

- (id)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.documentTitle = [UITextField new];
        self.documentExtension = [UILabel new];

        self.documentTitle.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"Document title"];
        self.documentTitle.font = [UIFont boldSystemFontOfSize:[UIFont systemFontSize] + 1];
        self.documentTitle.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
        self.documentTitle.borderStyle = UITextBorderStyleRoundedRect;
        self.documentTitle.returnKeyType = UIReturnKeyDone;
        self.documentTitle.keyboardType = UIKeyboardTypeDefault;

        self.documentExtension.text = @".pdf";
        self.documentExtension.backgroundColor = [UIColor clearColor];

        [self addSubview:self.documentTitle];
        [self addSubview:self.documentExtension];
    }

    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];

    UIEdgeInsets padding = UIEdgeInsetsMake(12, 10, 12, 10);
    CGRect layoutRect = UIEdgeInsetsInsetRect(self.bounds, padding);
    CGPoint p = layoutRect.origin;

    CGSize tmpSize;
    CGFloat tfSpaceHorizontal = 6;
    CGFloat tfSpaceVertical = 6;

    CGSize tmpSizeDocumentExtension = [self.documentExtension sizeThatFits:CGSizeMake(CGRectGetWidth(layoutRect), 1000)];

    tmpSize = CGSizeMake(
            roundf((CGRectGetWidth(layoutRect) - tfSpaceHorizontal - tmpSizeDocumentExtension.width)),
            32
    );

    self.documentTitle.frame = CGRectMake(
            p.x, p.y,
            tmpSize.width,
            tmpSize.height
    );

    p.x += tmpSize.width + tfSpaceHorizontal;
    CGFloat addedHeight = roundf(tmpSize.height/2 - tmpSizeDocumentExtension.height/2);
    addedHeight = addedHeight > 0 ? addedHeight : 0;
    p.y += addedHeight;

    self.documentExtension.frame = CGRectMake(
            p.x,
            p.y,
            tmpSizeDocumentExtension.width,
            tmpSizeDocumentExtension.height
    );
}

1 个答案:

答案 0 :(得分:0)

在iOS 7中,每个视图控制器都使用全屏布局,因此坐标系统会在状态栏下启动,因此请注意定位视图时。

Apple的iOS 7 UI Transitioning Guide中提到了这一点。