Customize title in UITableView

I am trying to customize the title in a section in a UITableView. I could not find this in the storyboard, so I started adding UIView and UILabel.

Here is my code:

func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    let headerView = UIView(frame: CGRectMake(0,0, tableView.frame.size.width, 60))
    headerView.backgroundColor = UIColor.cyanColor()
    headerView.layer.borderColor = UIColor.whiteColor().CGColor
    headerView.layer.borderWidth = 1.0;

    let headerLabel = UILabel(frame: CGRectMake(5,2, tableView.frame.size.width - 5, 30))
    headerLabel.text = sectionTitle  (this is a variable)
    headerLabel.textAlignment = NSTextAlignment.Center
    headerLabel.font = UIFont(name: "AvenirNext", size: 18)
    headerLabel.textAlignment = NSTextAlignment.Center;
    headerView.addSubview(headerLabel)

    return headerView
    }

func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 60
    }

It looks like this: enter image description here

I am looking for a way to find UILabel vertically and horizontally in the center of the view. How can I do it? Does it always change font and size?

Also, if I could do this in a storyboard? It is very difficult to make the header programmatically.

+4
source share
3 answers

You can drag a UIView onto your UITableView in your storyboard. The view will be automatically added as the title of the UITableView. See the hierarchy of views in the figure below.

UILabel :

- :

Storyboard view

EDIT:

, . , func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView?, .

@IBOutlet UIView *headerView;, , . UIView UIViewController/UITableViewController (. ).

UIView. IBOutlet UILabel, .

Storyboard section title header

+20

.

:

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
    {
        UIView *container = [UIView new];
        UILabel *label = [UILabel new];
        label.translatesAutoresizingMaskIntoConstraints = NO;
        [container addSubview:label];
        [container addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-(20)-[label]-(10)-|" options:0 metrics:nil  views:@{@"label" : label }]];
        [container addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-(15)-[label]-(3)-|" options:0 metrics:nil   views:@{@"label" : label}]];

        label.text = [NSString stringWithFormat:@"Section %ld", section];
        return container;   
    }

, , XIB.

Swift:

let xConstraint = NSLayoutConstraint(item: label, attribute: .CenterX, 
                                   relatedBy: .Equal,
                                      toItem: container, attribute: .CenterX, 
                                  multiplier: 1.0, constant: 0.0);
container.addConstraint(xConstraint);

let yConstraint = NSLayoutConstraint(item: label, attribute: .CenterY, 
                                   relatedBy: .Equal, 
                                   toItem: container, attribute: .CenterY, 
                                  multiplier: 1.0, constant: 0.0);
container.addConstraint(yConstraint);

let hConstraint = NSLayoutConstraint(item: label, attribute: .Height, 
                                   relatedBy: .Equal,
                                      toItem: container,attribute: .Height, 
                                  multiplier: 1.0, constant: 0.0);
container.addConstraint(hConstraint);

let wConstraint = NSLayoutConstraint(item: label, attribute: .Width, 
                                   relatedBy: .Equal,
                                      toItem: container, attribute: .Width, 
                                  multiplier: 1.0, constant: 0.0);
container.addConstraint(wConstraint);
+1

, , , xib

-1

Source: https://habr.com/ru/post/1625948/


All Articles