Auto-Resize UITableView Headers on Rotate (Mostly on iPad)

瘦欲@ 提交于 2019-12-05 00:45:17

Not a very good fix. But works :

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
    [super willAnimateRotationToInterfaceOrientation:toInterfaceOrientation duration:duration];
    [mTableView reloadData];
}

I faced the same issue recently. The trick was to use a custom view as the headerView of the table. Overriding layoutSubviews allowed me to control the layout at will. Below is an example.

#import "TableSectionHeader.h"

@implementation TableSectionHeader

- (id)initWithFrame:(CGRect)frame title:(NSString *)title
{
    self = [super initWithFrame:frame];
    if (self) {

        self.backgroundColor = [UIColor clearColor];

        // Initialization code
        headerLabel = [[UILabel alloc] initWithFrame:frame];
        headerLabel.text = title;

        headerLabel.textColor = [UIColor blackColor];
        headerLabel.font = [UIFont boldSystemFontOfSize:17];
        headerLabel.backgroundColor = [UIColor clearColor];

        [self addSubview:headerLabel];
    }
    return self;
}

-(void)dealloc {

    [headerLabel release];

    [super dealloc];
}

-(void)layoutSubviews {

    [super layoutSubviews];

    NSInteger xOffset = ((55.0f / 768.0f) * self.bounds.size.width);

    if (xOffset > 55.0f) {
        xOffset = 55.0f;
    }

    headerLabel.frame = CGRectMake(xOffset, 15, self.bounds.size.width - xOffset * 2, 20);
}

+(UIView *) tableSectionHeaderWithText:(NSString *) text bounds:(CGRect)bounds {
    TableSectionHeader *header = [[[TableSectionHeader alloc] initWithFrame:CGRectMake(0, 0, bounds.size.width, 40) title:text] autorelease];
    return header;
}

+(CGFloat) tableSectionHeaderHeight {
    return 40.0;
}
@end

I'd love to get a real answer to this, but for now, I've just re-worked my UITableView so that my "headers" are just cells inside the table. Resizing has no issues that way.

I have created UIView subclass where I've used visual constraints to stick the subview to the sides of the screen. And rotation is all fine.

class MLFlexibleView: UIView {

override init(frame: CGRect) {
    super.init(frame: frame)

    self.addSubview(self.segmentedControl)
    self.setUpConstraints()
}

func setUpConstraints() {
    let views = ["view" : self.segmentedControl]

    NSLayoutConstraint.activate(NSLayoutConstraint.constraints(withVisualFormat: "V:|-7-[view]-7-|", options: [], metrics: nil, views: views))
    NSLayoutConstraint.activate(NSLayoutConstraint.constraints(withVisualFormat: "H:|-15-[view]-15-|", options: [], metrics: nil, views: views))
}

required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

let segmentedControl:UISegmentedControl = {
    let segmentedControl = UISegmentedControl(items: ["Searches".localized, "Adverts".localized])
    segmentedControl.selectedSegmentIndex = 0
    segmentedControl.tintColor = UIColor.white
    segmentedControl.translatesAutoresizingMaskIntoConstraints = false
    return segmentedControl
}()

}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!