[iOS] Automatic layout on the UITableView header

Source: Internet
Author: User

turn from: http://www.itstrike.cn/Question/3a52cc4a-db02-4121-b92e-14ff7cce1ade.html


I've been looking for a clear answer to how to add an automatic layout to UITableView. So far, my code looks as follows:

-(UIView *) TableView: (UITableView *) TableView viewforheaderinsection: (nsinteger) Section {
uinib *nib = [uinib nibwithnibname:@ "Homeheaderview" bundle:nil];
UIView *headerview = (UIView *) [nib instantiatewithowner:self options:nil][0];
[Headerview.layer setcornerradius:6.0];
[Headerview Settranslatesautoresizingmaskintoconstraints:no];

Nsdictionary *viewsdictionary = nsdictionaryofvariablebindings (Headerview);
Nsmutablearray *headerconstraints = [[Nsmutablearray alloc] init];
[Headerconstraints addobject:[nslayoutconstraint constraintswithvisualformat:@ "H:|-[headerView]-|" options:0 Metrics:nil Views:viewsdictionary]];
[Headerconstraints addobject:[nslayoutconstraint constraintswithvisualformat:@ "V:|-[headerView]-|" options:0 met Rics:nil Views:viewsdictionary]];
[Self.actionstableview addconstraints:headerconstraints];
[Self.view Addsubview:headerview];
Tableview.tableheaderview = Headerview;
[Headerview Layoutsubviews];

Nslayoutconstraint *centerx = [Nslayoutconstraint constraintwithitem:headerview Attribute:nslayoutattributecenterx Relatedby:nslayoutrelationequal ToItem:self.view Attribute:nslayoutattributecenterx multiplier:1 constant:0];
Nslayoutconstraint *centery = [Nslayoutconstraint constraintwithitem:headerview attribute:nslayoutattributecentery Relatedby:nslayoutrelationequal ToItem:self.view attribute:nslayoutattributecentery multiplier:1 constant:0];
Nslayoutconstraint *width = [Nslayoutconstraint constraintwithitem:headerview attribute:nslayoutattributewidth Relatedby:nslayoutrelationequal toitem:nil attribute:0 multiplier:1 constant:300];
Nslayoutconstraint *height = [Nslayoutconstraint constraintwithitem:headerview attribute:nslayoutattributeheight Relatedby:nslayoutrelationequal toitem:nil attribute:0 multiplier:1 constant:90];
[Self.view Addconstraints:@[centerx, centery, width, height]];

return headerview;
}

I basically have a nib file with my head view and I want to center that nib in my uitableviewheader. Want to grow and shrink accordingly in both longitudinal and transverse directions. I'm really not sure if the constraint is set correctly. Not sure if I toitem should be the View Controller view or the connection itself.

I don't know if I was going to add headerview as a child view or the connection itself to any view controller.

Or, I'm not sure if setting Tableview.tableheaderview = Headerview is enough.

I really have no idea what such a thing is best for. I'm not sure if all this can be done in IB as well. Currently, with the code you see this error:

' Auto Layout still required after executing-layoutsubviews. UITableView ' s implementation of-layoutsubviews needs to call super. '

Precisely because of this error, add [Headerview layoutsubviews]

Thoughts about it. Thanks for the first! Workaround 1:

The real problem is that you confuse viewforheaderinsection: With the Headerview in the table. They are irrelevant.

The former is a section header. The view you return from the delegate method.

The latter is the header of the table. You may be in the settings view, you viewdidload.

In the normal way to manipulate constraints. But they should just be their child view internal restrictions. When you create it, it is not in the view of your interface. Its size and position do not depend on you during this period. If it is a section header, it will automatically resize to fit correctly (depending on the width of the table and the height of the caption or the declaration of the delegate). If it is the header, you can put it to the absolute height, but its width will be resized to fit correctly.

Here is a complete example of the build with its child view on the Inner constraint section header.

-(UIView *) TableView: (UITableView *) TableView 
        viewforheaderinsection: (Nsinteger) Section {
    uitableviewheaderfooterview* h =
        [TableView dequeuereusableheaderfooterviewwithidentifier:@ "Header"];
    if (![ H.tintcolor isequal: [Uicolor Redcolor]] {
        H.tintcolor = [Uicolor redcolor];
        H.backgroundview = [UIView new];
        H.backgroundview.backgroundcolor =
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.