简体   繁体   English

将UILabel居中于UITableView标头中

[英]Centering a UILabel inside of a UITableView Header

I added my UIView as a UITableView header, but when I did, my constraints fell apart. 我将UIView作为UITableView标头添加,但是当我这样做时,我的约束就崩溃了。 I am trying to have my chapterVerseLabel centered inside the circle as so : 我正在尝试将我的chapterVerseLabel在圆圈内,如下所示:

在此处输入图片说明

I accomplish this thus so far by doing this : 到目前为止,我是这样做的:

    var allConstraints = [NSLayoutConstraint]()
    let views = ["tableView" : tableView, "containerView" : containerView, "chapterVerseLabel" : chapterVerseLabel]
    allConstraints += NSLayoutConstraint.constraintsWithVisualFormat("V:|-70-[chapterVerseLabel]-70-|", options: [], metrics: nil, views: views)
    NSLayoutConstraint.activateConstraints(allConstraints)

But I'm not sure how to get it centered as well. 但是我不确定如何使它居中。 It seems all my attempts to center it, don't abide by the stricter rules of centering something within a UITableView Header. 似乎所有尝试将它居中的尝试都没有遵守在UITableView Header中居中的更严格的规则。

All ideas welcome. 欢迎所有想法。


Update 更新资料

I tried adding a .CenterX value : 我尝试添加.CenterX值:

    let leftConstraint = NSLayoutConstraint(item: chapterVerseLabel, attribute: .CenterY, relatedBy: .Equal, toItem: containerView, attribute: .CenterY, multiplier: 1.0, constant: 0.0)
    containerView.addConstraint(leftConstraint)
    let rightConstraint = NSLayoutConstraint(item: chapterVerseLabel, attribute: .CenterX, relatedBy: .Equal, toItem: containerView, attribute: .CenterX, multiplier: 1.0, constant: 0.0)
    containerView.addConstraint(rightConstraint)

But this is the result : 但这是结果:

在此处输入图片说明

Right now, the chapterVerseLabel sits within my containerView, which is assigned as the tableView.tableHeaderView . 现在,ChapterVerseLabel位于我的containerView中,该容器被分配为tableView.tableHeaderView Like so : 像这样:

tableView.tableHeaderView = containerView

Any ideas? 有任何想法吗?

The problem with your constraint are that you have already bounded the label by giving Top and Bottom constraints. 约束的问题在于,您已经通过给出顶部和底部约束来限制标签​​。 Also i dont see leading and trailing constraints You actually have to align the label vertically or horizontally or both based on your requirement. 另外,我也看不到前导约束和尾随约束,实际上您必须根据需要垂直或水平对齐标签,或同时对齐两者。

NSLayoutConstraint *yCS =[NSLayoutConstraint constraintWithItem:chapterVerseLabel attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:containerView attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:0.0];
[containerView addConstraint:yCS];

NSLayoutConstraint *xCS =[NSLayoutConstraint constraintWithItem:chapterVerseLabel attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:containerView attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0.0];
[containerView addConstraint:xCS];

Assuming containerView is your header view which gets returned from headerforView delegate method. 假设containerView是您的标头视图,它是从headerforView委托方法返回的。 You need to provide height and width to the label. 您需要为标签提供高度和宽度。

There is a uiview at the center. 中心有一个uiview。 If you want to add a view(1/4) to the circular view at the center you can not do it with the visual format. 如果要向中心的圆形视图添加视图(1/4),则无法使用视觉格式。 I assume you want to center chapterVerseLabel . 我假设您想将chapterVerseLabel So: 所以:

chapterVerseLabel.translatesAutoresizingMaskIntoConstraints = false
yourCircularView.addsubView(chapterVerseLabel)
let xCenterConstraint = NSLayoutConstraint(item: chapterVerseLabel, attribute: .CenterX, relatedBy: .Equal, toItem: view2, attribute: .CenterX, multiplier: 1, constant: 0)
superview.addConstraint(xCenterConstraint)

let yCenterConstraint = NSLayoutConstraint(item: chapterVerseLabel, attribute: .CenterY, relatedBy: .Equal, toItem: view2, attribute: .CenterY, multiplier: 1, constant: 0)
superview.addConstraint(yCenterConstraint)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM