None option not included in simulated metrics - ios

I've been trying to load my headerView.xib file onto my BNRItemViewControlelr file which already has its on view.
I am just adding my headerView file onto the BNRItemViewController view. However when I look in the simulated metrics to try and to edit the size of the view there ins't a none option, instead just a freeform which still doesn't change the actual view at runtime. Is there a way I could change it?
(I can't currently post the pictures because I need 10 reputation points)

Just set the frame directly in the code, like this:
- (void)viewDidLoad
{
[super viewDidLoad];
[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:#"UITableViewCell"];
// Adding top margin to the header view
UIEdgeInsets inset = UIEdgeInsetsMake(20, 0, 0, 0);
self.tableView.contentInset = inset;
UIView *header = self.headerView;
// making the frame for the header view
header.frame = CGRectMake(0, 0, 400, 57);
[self.tableView setTableHeaderView:header];
}
I actually found the answer in this stackoverflow thread: Adding constraint to a UITableVIew headerview
to add top margin: Top margin on UITableViewController
we are using UIEdgeInsets

Related

Set UITableViewHeaderFooterView margin values (on scrolling)

I build a UITableView with section header that can expand a specific section. I am using a custom view from a .xib file for a UITableViewHeaderView. I want to set margin values of the HeaderView so that it is not full width and stays on top when scrolling down (not full height).
As you can see in the animation the view has full width - is it possible to add a margin to the header that there is some space between the HeaderView and the edge of the screen.
I want to reduce the height of the sticky header if the UITableView is scrolled - so that there is just the text of the button visible on top of the screen. I have implemented the following delegate command from UIScrollView which works fine, but it reduces the margin for the first HeaderView so that it moves behind the navigation bar (see in animation) - how can I avoid that?
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
CGFloat sectionHeaderHeight = 20;
if (scrollView.contentOffset.y<=sectionHeaderHeight&&scrollView.contentOffset.y>=0) {
scrollView.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y, 0, 0, 0);
} else if (scrollView.contentOffset.y>=sectionHeaderHeight) {
scrollView.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, 0, 0, 0);
}
}
You could put a subview with a margin in your headerview.
Setting the height is certainly possible and is done by setting the frame, but you could also make your navigation bar non-translucent, which would save you a ton of coding.
[self.navigationController.navigationBar setTranslucent:NO];
If you want to change the height, your code would look something like:
CGRect frame = header.frame;
frame.size.height = 44;
header.frame = frame;
[self.tableView setTableHeaderView:header];

UITableView header height will not change in storyboard

I have a Storyboard that is using a UITableViewController. I have added a UIView as the UITableView header and set the height to 200. But, for some reason, when I preview it, the header is huge! looks to be about 540 high (header is white):
Here are my settings:
It looks correct in the storyboard preview. What could be causing it to be so huge and prevent my height setting from working?
Apple figured it out. Here is what they said:
Because the frame of a view can not be customized per-size class, you
need to make changes to your header view's frame while editing the
[wAny hAny] size class.
I was in the [wCompact hRegular] mode, which apparently you cannot set frame sizes in.
Just solved this question, try this:
UIView *v = self.tableView.tableHeaderView;
CGRect fr = v.frame;
fr.size.height = [UIScreen mainScreen].bounds.size.height -100;
v.frame = fr;
[self.tableView updateConstraintsIfNeeded];
Can't set constraints of UItableView's tableHeaderView height by autolayout in Storyboard
For me in order to make it work, I needed to create an IBOutlet reference to the header view. Then I added in the viewDidLoad:
self.header.frame = CGRectMake(0, 0, self.tableView.frame.size.width, 40);
self.tableView.sectionHeaderHeight = 0;
You have to take a seperate NibFile and resize the nib file as per your required size (Resize the height manually do not use attribute inspector.). Change Simulated metrics to freeform in attribute inspector.
Then You can customize the HeaderView .
static NSString *SectionHeaderViewIdentifier = #"SectionHeaderViewIdentifier";
#define HEADER_HEIGHT 200
- (void)viewDidLoad {
[super viewDidLoad];
self.tableView.sectionHeaderHeight = HEADER_HEIGHT;
// Set up default values.
self.tableView.sectionHeaderHeight = HEADER_HEIGHT;
UINib *sectionHeaderNib = [UINib nibWithNibName:#"SectionHeaderView" bundle:nil];
[self.tableView registerNib:sectionHeaderNib forHeaderFooterViewReuseIdentifier:SectionHeaderViewIdentifier];
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
UIView *sectionHeaderView = [self.tableView dequeueReusableHeaderFooterViewWithIdentifier:SectionHeaderViewIdentifier];
//You can customize your header view
return sectionHeaderView;
}

Grouped UITableView 35 point / pixel top inset / padding

Please note, I'm going to refer to points as pixels in this question.
I have a grouped UITableView with 3 sections, each with a 40 pixel tall header view. The first header in the table view seems to be given a y position of 35 pixels by the system.
I've tried messing around with automaticallyAdjustsScrollViewInsets and a few other iOS 7 automatic pieces, but to no avail.
Why is my UITableView content being inset by 35 pixels?
EDIT: I've seen this answer and many other threads on this. I have valid headers and header heights. Also, setting the default to FLT_MIN, 0.01f, 1.0f or 100.0f doesn't fix the problem.
Here is my header implementation:
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
UIView *header = (self.headerViews.count > section ? self.headerViews[section] : nil);
return (header.viewHeight ? : FLT_MIN);
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *header = (self.headerViews.count > section ? self.headerViews[section] : nil);
return (header.viewHeight ? header : nil);
}
I'm also setting:
- (BOOL)automaticallyAdjustsScrollViewInsets{return NO;}
and
[self setSectionHeaderHeight:FLT_MIN];
It seems that this:
[self setTableHeaderView:[[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, width, FLT_MIN)]];
In init, where self is a UITableView, will get rid of the top 35 points.
Yep,i get this problem when i set tableview.tableFooterView and custom set section header height, then first section height has over 35 points.
Then i add a
tableView.tableHeaderView = UIView(frame: CGRect(x: 0, y: 0, width: 0, height: CGFloat.leastNormalMagnitude))
worked for me.
There are multiple ways of getting the same issue you are getting and therefore, multiple solutions. I will try to list the ones I know with their corresponding solutions. Most of them are copied from this question.
UITableView inside a UIViewController
This is a common one that trips people off because they think the problem is related to their UITableView and most of the time is actually the parent UIViewController.
If your VC is embedded on a NavigationController. You will get a 35 points y offset as mentioned here.
Solutions
In Xcode Version > 5 on VC untick Extended Edges "Under Top Bars under the Attributes Inspector to remove the top UITableView content inset.
Constraints: Your VC has a main view where all the other subviews are laid including your UITableView. You need to make sure that all constraints from your UITableView are explicitily set and satisfied/non-ambiguous.
Set self.navigationController.navigationBar.translucent = YES;
Set self.automaticallyAdjustsScrollViewInsets = NO;. This one can also be set on the storyboard by unchecking the Adjust Scroll View Insets checkbox for the view controller layout.
Set self.edgesForExtendedLayout = UIRectEdgeNone;
General UITableView/UITableViewController
Set the tableView.separatorInset = UIEdgeInsetsZero;
Set the tableView.contentInset = UIEdgeInsetsMake(-35, 0, 0, 0); Pay attention to the -35. The negative number offsets the view.
Declare this method (Can also be used for FooterSection):
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
return CGFLOAT_MIN;
}

UITableView headerView scroll offset

I'm facing a problem with UITableView and it's property tableHeaderView.
I want to have the tableHeaderView to behave like UISearchBar, i.e. the content offset should be the of tableHeaderView.
Setting contentOffset, etc. didn't help when the table view wouldn't fill the view's frame.
Here's a screenshot of the current behavior:
(source: tubtub.de)
And how I'd like it to have:
(source: tubtub.de)
I'm inserting the headerView in viewDidLoad as follows:
- (void)viewDidLoad
{
[super viewDidLoad];
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
headerView.backgroundColor = [UIColor redColor];
self.tableView.tableHeaderView = headerView;
}
Any help or hint is highly appreciated. Thanks!
EDIT: I made it working by utilizing UIScrollViewDelegate and subclassing UITableView. Check it out on the github repo provided.
You can use setContentOffset: by headerView height.
This contentOffset will only happen when you have enough number of data to be off by your headerView height. i.e) If tableView is not scrollable because you have only few data like one in your screenshot, the headerView is still visible. However if you have lots of number of data that can't not be displayed in the screen it will have contentOffset.
Try this with 20 rows. You will see what I mean.
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
headerView.backgroundColor = [UIColor redColor];
self.tableView.tableHeaderView = headerView;
[self.tableView setContentOffset:CGPointMake(0, 44)];
With only 3 rows, this contentOffset won't work and it might be desired behavior in your case because you don't want to hide the searchBar when you have extra space to display.
I came up with implementing my own solution.
I utilize UIScrollViewDelegate and came up with a subclass of UITableView for calculating the contentSize dynamically.
Take a look at the github repo here.
I would double check the frame for the table itself. By setting the headerView's frame to start at 0,0, you are specifying that it should have its origin in the top left of the the view that is designated for the table.

How to change the size of tableview programmatically?

I try to change the size (the width) of a table viewcontroller (the panel with text over syria):
I have tried :
-(void)viewWillAppear:(BOOL)animated
{
self.tableView.frame = CGRectMake(0, 310, self.view.frame.size.width,200);
}
but it doesn't work. Do you know how can I reduce the width of the TableView ?
Thanks for your help !
You will have to add the table view as a subview to your view controllers view with the new size in viewWillAppear
Check the view frame width to make sure it is actually less than 320 before setting it. Most likely you are setting the table view width back to 320 which is the entire width.
Also if you are just setting the width of the table I would recommend doing the following.
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
CGRect tableViewFrame = self.tableView.frame;
tableViewFrame.width = 250;
self.tableView.frame = tableViewFrame;
}

Resources