Skip to content

Instantly share code, notes, and snippets.

@AlessioAnesa
Forked from andreacremaschi/ UITableView+Header
Last active August 29, 2015 14:14
Show Gist options
  • Save AlessioAnesa/64c2e9052523de24736e to your computer and use it in GitHub Desktop.
Save AlessioAnesa/64c2e9052523de24736e to your computer and use it in GitHub Desktop.
UITableView convenience classes for resizing header and footer with autolayout.
The MIT License (MIT)
Copyright (c) 2014, Alessio Anesa
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
Pod::Spec.new do |s|
s.name = "UITableView+Header"
s.version = "0.0.2"
s.summary = "UITableView convenience classes for resizing header and footer with autolayout."
s.description = <<-DESC
DESC
s.homepage = "https://gist.github.com/andreacremaschi/833829c80367d751cb83"
# s.license = "MIT"
s.license = { :type => "MIT", :file => "LICENSE" }
s.author = { "Andrea Cremaschi" => "andrea.cremaschi@midainformatica.it" }
s.platform = :ios, "6.0"
s.source = { :git => "https://gist.github.com/833829c80367d751cb83.git", :tag => "0.0.1" }
s.source_files = "*.{h,m}"
s.exclude_files = "Classes/Exclude"
s.public_header_files = "*.h"
s.requires_arc = true
end
#import <UIKit/UIKit.h>
@interface UITableView (HeaderView)
- (void) sizeHeaderToFit;
- (void) sizeFooterToFit;
@end
@interface UIView (FittingSize)
- (BOOL)sizeToFitVertically;
@end
#import "UITableView+HeaderView.h"
static NSString *ConstraintIdentifier = @"WidthConstraintIdentifier";
@implementation UITableView (HeaderView)
- (void) sizeHeaderToFit
{
UIView *headerView = self.tableHeaderView;
[self updateWidthConstraintForView:headerView];
if ([headerView sizeToFitVertically])
self.tableHeaderView = headerView;
}
- (void) sizeFooterToFit
{
UIView *footerView = self.tableFooterView;
[self updateWidthConstraintForView:footerView];
if ([footerView sizeToFitVertically])
self.tableFooterView = footerView;
}
- (void)updateWidthConstraintForView:(UIView *)view
{
NSLayoutConstraint *constraint;
for (constraint in view.constraints)
{
if (constraint.identifier && [constraint.identifier isEqualToString:ConstraintIdentifier])
{
break;
}
else
constraint = nil;
}
if (!constraint)
{
constraint = [NSLayoutConstraint constraintWithItem:view
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationEqual
toItem:nil
attribute:NSLayoutAttributeNotAnAttribute
multiplier:1.0
constant:self.bounds.size.width];
constraint.identifier = ConstraintIdentifier;
[view addConstraint:constraint];
}
else
{
constraint.constant = self.bounds.size.width;
}
}
@end
@implementation UIView (FittingSize)
- (BOOL)sizeToFitVertically
{
CGSize size = [self systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];
if (CGSizeEqualToSize(self.frame.size, size))
return NO;
self.frame = ({
CGRect headerFrame = self.frame;
headerFrame.size = size;
headerFrame;
});
return YES;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment