Reputation: 6490
I want to increase gap between header and cell of UITableView.
I have searched a lot and i found this solution,
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 20.0;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
//Adding UIView and it's Background Image...
UIView *tempView=[[UIView alloc]initWithFrame:CGRectMake(0,0,300,20)];
UIImageView *BgImg=[[UIImageView alloc]initWithFrame:CGRectMake(0,0,300,20)];
BgImg.image= [UIImage imageNamed:@"venueTitleBg.png"];
[tempView addSubview:BgImg];
//Adding UIImageView...
UIImageView *flag=[[UIImageView alloc]initWithFrame:CGRectMake(10,3,20,12)];
flag.image= [UIImage imageNamed:@"flag_small_united-kingdom.png"];
[tempView addSubview:flag];
//Adding UILabel...
UILabel *tempLabel=[[UILabel alloc]initWithFrame:CGRectMake(40,1,300,15)];
tempLabel.backgroundColor=[UIColor colorWithRed:15.0/255.0 green:15.0/255.0 blue:30.0/255.0 alpha:1.0];
tempLabel.textColor=[UIColor whiteColor];
tempLabel.font=[UIFont systemFontOfSize:11];
tempLabel.text=@"United Kingdom";
[tempView addSubview: tempLabel];
return tempView;
}
I tried this code, it is working fine in simulator but it doesn't show any spaces, when i run it on my device.
Here is snapshot of Simulator: (I can see gap between Header and cell..)
:
Thanks in advance.
Upvotes: 3
Views: 7083
Reputation: 171
Swift 4.2
1- Create a custom header class give it a clear background.
class CustomHeader: UITableViewHeaderFooterView{
override func awakeFromNib() {
super.awakeFromNib()
backgroundView = UIView(frame: self.bounds)
backgroundView!.backgroundColor = .clear
}
}
2- Create a .xib file and give assign the class you just created.
3- Create a background view inside your custom header in .xib file
4- Give a bottom padding to this background view and behave it as container of your header view subviews.
5- Do not forget register your custom header to your tableview.
tableView.register(UINib(nibName: "NibName", bundle: nil), forHeaderFooterViewReuseIdentifier: "Identifier")
6- Use the header whenever you want.
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let header = tableView.dequeueReusableHeaderFooterView(withIdentifier: "HeaderIdentifier") as? CustomHeader
// Customize your header
return header
}
7- If you don't need customization for your header and you use only one custom header, you can use this short cut where you set your table view.
yourTableView.tableHeaderView = tableView.dequeueReusableHeaderFooterView(withIdentifier: "HeaderIdentifier") as? CustomHeader
Upvotes: 0
Reputation: 4500
I think the problem is you are using images to load the header view. As you are running the code in iPhone 4s which is the retina display phone. It's important that you add @2x image of all the images you have used below code :
BgImg.image= [UIImage imageNamed:@"venueTitleBg.png"];
flag.image= [UIImage imageNamed:@"flag_small_united-kingdom.png"];
Please add the @2x image and try.
Upvotes: 0
Reputation: 7226
Well the issue you might be facing is either you are using the iphone 5 device and ur running the simulator for iphone 4 or vice-versa. Also you might have your Auto-layout on. Also check to increase your height of the heightForHeaderInSection
to say 50.0 or more as the height in the device looks small anyways .
Upvotes: 0
Reputation: 408
Try this:
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 50.0;
}
Upvotes: 3