I am currently trying to remove the top or bottom view from my tableview but the size of the whole tableview isnt just right. Here is what i did so far.
Created a TableviewController inside the Storyboard
Created a Class derived from UITableViewController and set
everything up like delegate etc.
Dragged a view to the top of the tableview inside the Storyboard
Created and connected an outlet named topview for this
In Viewwillappear i will remove that topview dependent of some value so i tried some things
-(void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
if(foo){
[self.topView setHidden:YES];
// or
[self.topView removeFromSuperview];
// or
[self.topView setFrame:CGRectMake(0, 0, 0, 0)];
}
}
For example if i remove the view from superview the topview isnt available anymore but there is a big gap on top .. how can i remove this top view from my tableview without having this gap ?
I put some Screenshots to describe my Problem:
If you put it inside the tableview it is probably the tableHeaderView. Try this:
self.tableView.tableHeaderView = nil;
Should remove the view and update the tableview accordingly.
[self.tableview reloadData]
after removing topview or if you're using autolayout then you can use [self.view updateConstraints];
Related
I am having a (UIView and UICollectionView in a UIScrollView) as 1stVC. I have to scroll both at the same time for which I already unable the collection view scrolling. So first time When I launch that screen I am able to scroll the whole view, But when I push to (next ViewController) 2ndVC and then press Back Button to 1stVC my scroll view is not preforming it got Freeze.
Tried this Method:-
-(void)viewDidLayoutSubviews{
[super viewDidLayoutSubviews];
dispatch_async(dispatch_get_main_queue(), ^ {
CGRect contentRect = CGRectZero;
for(UIView *view in self.scrollView.subviews)
contentRect = CGRectUnion(contentRect,view.frame);
self.scrollView.contentSize = contentRect.size;
});
}
Tried this Method:-
-(void)viewDidLayoutSubviews{
[super viewDidLayoutSubviews];
[_scrollView setContentSize:CGSizeMake(_scrollView.frame.size.width, _scrollView.frame.size.height)];
}
And For Getting Dynamic Height of the Collection view I have Done This
CGFloat height = _collectionCompass.contentSize.height;
Which I Have provided to ScollView ContentSize.
_scrollView.contentSize = CGSizeMake(_scrollView.frame.size.width, height);
Please help me.
What I really suggest it's to remove scroll view and do all stuff in collection view, since with scroll view you're missing some of benefits (e.g. cells reusing). Also collection view in scroll view it's a bad practice.
From Apple style guide:
Don’t place a scroll view inside of another scroll view. Doing so creates an unpredictable interface that’s difficult to control.
If your UI as list, you can add view as header of collection view or as another cell. With this approach you can remove the code from viewDidLayoutSubviews
I'm using Masonry to set the constraints of the subviews.
The view controller is simple.
in viewDidLoad
-(void)viewDidLoad
{
[self.view addSubview: self.tableView];
self.view.backgroundColor = [UIColor whiteColor];
[self.tableView addSubview: self.botBtn];
[self.botBtn makeConstraints:^(MASConstraintMaker *make) {
make.bottom.equalTo(self.tableView.bottom).with.offset(-20);
make.centerX.equalTo(self.tableView);
}];
}
The frame for the table view is CGRectMake(0,0,screen_width,screen_height),
but the button is outside of the screen and just on the top of the tableview.
I set the tableview frame in it's init method.
The button stays in the mid of the view horizontally so the autolayout seems work, but why it is positioned on the top of the tableview instead of staying on the bottom.
Firstly well done for using Masonry :D.
To your question, I don't think you're adding the button in the right way, I'm guessing you want to add a button to bottom of the tableview, so when you scroll to the bottom of the tableview you will see your button? If so, just create a UIView with a button and set the tableview's footer view, i.e. [self.tableView setTableFooterView:newView]; as described in this question, Adding a button at the bottom of a table view
Let me know if this is not what you're actually looking for, good luck.
I have a UITableViewController and I put a UIView right under the navigation item and above the actual table. The problem that I have is that the view scrolls with the tableview.
How would I get it to behave exactly like the nav bar, and have the items in the tableview scroll behind it.
Rather than having the view scroll, it should remain in its position and have everything go behind it. Sorry for reiterating, but I've found thats necessary sometimes.
The view you're placing above the cell in the storyboard becomes the table view's tableHeaderView.
You can make the header view appear fixed by resetting its frame.origin to the table view's bounds.origin every time the table view lays out its subviews:
- (void)viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
UIView *header = self.tableView.tableHeaderView;
CGRect frame = header.frame;
frame.origin = self.tableView.bounds.origin;
header.frame = frame;
}
Result:
Assuming you don't want the map view to move then you could set its user interaction to false.
Alternatively you could set the header of your tableView (if you only have one section) to the map view.
This question should not be mixed up with this here.. These are two different things.
There is a good example how to use a UITableView Header on SO.
This all works fine and the main header is fixed on top as long as the style is set to plain.
But if I use sections, the main header no longer sticks to top and moves away while scrolling to the bottom.
In this method, I am returning the header for each section.
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
In this method I am setting the height for the header section above:
- (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
In this method, I am setting the real table header.
- (void)viewWillAppear:(BOOL)animated {
...
self.recordTableView.tableHeaderView = headerView;
}
Is it even possible having a fixed table header, while using sections?
What is an alternative solution to this please?
If you want a UITableViewController (static cells/keyboard handling) and have a fixed header then you should use Containment. You can do this from a Storyboard by setting up a UIViewController with your fixed header and then using a Container View to embed the UITableViewController.
Once you have your containing view setup, you right-click drag from the Container View to the View Controller you want to embed - the UITableViewController in this case.
You can access and get a reference to the contained View Controller (the UITableViewController) from the Container View Controller by implementing the prepareForSegue:sender: method.
There’s no way to maintain the header of a tableView fixed, but
an useful approach when you need a unique header, is to use a UIViewController rather than a UITableViewController, and set the header (UIView) out from the tableView.
Something like this:
If you want to keep the class as a UITableViewController you can add your header as a subview to the tableview's superview. You will have to also push the tableview top inset down so your headerview doesnt hide the table.
Here is a sample code to put inside your tableViewController subclass (This example assumes your tableview controller is inside a navigation controller, so it pushes the view to below the navigation bar):
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
self.automaticallyAdjustsScrollViewInsets = NO;
}
-(void)addHeaderView{
CGFloat yPosition = self.navigationController.navigationBar.frame.origin.y + self.navigationController.navigationBar.frame.size.height;
mainHeaderView = [[UIView alloc] init];
const CGFloat mainHeaderHeight = 44;
[mainHeaderView setFrame:CGRectMake(0, yPosition, self.view.frame.size.width, mainHeaderHeight)];
mainHeaderView.backgroundColor = [UIColor redColor];
[self.tableView.superview addSubview:mainHeaderView];
[self.tableView setContentInset:UIEdgeInsetsMake(yPosition + mainHeaderHeight, self.tableView.contentInset.left, self.tableView.contentInset.bottom, self.tableView.contentInset.right)];
}
I haven't done this, but the first thing I would think to try is to place my tableview in a UIView and make my own header there in that UIView. Seems a trivial matter to make that view appear to be the header of the table and it would certainly stay put.
I have a UITableViewController. In the UITableView I have a subview - ImageView. ImageView this is my header of TableView. I want that header (ImageView) is always on top when I scroll.
I have the following code which work on IOS 5, but does not work on IOS 6:
- (Void) scrollViewDidScroll: (UIScrollView *) scrollView {
[ImageView setFrame: CGRectMake (0, self.tableView.contentOffset.y, 320, 100)];
}
So, what should I do to solve this problem on IOS 6?
The header and footer in UITableView and UICollection view are fixed inside there container scrollview. What you need to do is actually place a button outside of your UITableViewController and added after that controller either in Interface builder or when setting up your VC in viewDidload. This will then be above the scroll view. You might want to resize the UITableViewController so that the content doesnt pass under the button.