0

我在 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
    );
}
4

1 回答 1

0

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

这在 Apple 的iOS 7 UI 过渡指南中有所提及。

于 2013-09-20T13:25:29.153 回答