I have a UITableView showing chat messages. At the bottom of the UIViewController there is a subView containing a UITextField that the user can write his comment and a "post" UIButton. User taps textfield, the keyboard appears pushing up self.view, write his comment then taps post and tableView is reloaded. The new message appears nice after i scroll the tableView at the bottom. The problem is that in simulator it works fine but in the device the last cell is not fully shown.
Any ideas?Thanks!
tableView.contentInset = UIEdgeInsetsMake(0, 0, 10, 0); //values passed are - top, left, bottom, right
set bottom offset as per your needs.
try:
- (void)viewDidLoad
{
[super viewDidLoad];
....
tableView.contentInset = UIEdgeInsetsMake(0, 0, bottomValue, 0);
}
where bottomValue height your textField
Related
I have created a tableview in code as follows:
_myTableView = [[UITableView alloc] initWithFrame:
CGRectMake(160, 80, 140, 100) style:UITableViewStylePlain];
_myTableView.delegate = self;
_myTableView.dataSource = self;
_myTableView.scrollEnabled = YES;
[self.view addSubview:_myTableView];
It largely works as it should with the following exception. Because the results in the table vary, I manually adjust the height of the tableview so that it only takes up as much space as the returned rows need as follows:
-(void) changeTVHeight: (float) height {
//height calculated from number of items in array returned.
CGRect newFrame = CGRectMake(120, 80, 180, height);
self.myTableView.frame = newFrame;
}
This works great for shrinking the tableview if there aren't that many results.
However, if there are a lot of results, the tableview expands below the visible part of the screen or the keyboard. In this case, I would like to be able to scroll the Tableview to see the lower rows.
scrollEnabled is set to YES.
But while it does allow one to scroll a bit, the scroll is resisted so with effort you can scroll a little bit but due to rubber band effect you cannot get further than a few rows below the screen and you cannot tap on the lower rows.
I am using autolayout in storyboard for much of the screen. The overall screen scrolls fine but this merely moves the tableview anchored to the screen up and down. There are no constraints on this tableview but it is added as a subview of the view.
My question is how can I make the tableview scrollable so that it scrolls without resistance?
Thanks in advance for any suggestions.
Edit:
I tried adding the tableView to self.scrollView instead of self.view. This anchored the tableView to the scrollview so it is possible to scroll the whole screen down and see the bottom of the tableview. However, this is not ideal because the rest of the screen is empty way down and you can't see the context for the tableview. (It's an autocomplete for a textfield at top of screen.)
In contrast when the tableview is added to self.view, it is in correct place, it semi-scrolls or bounces. It just doesn't scroll down to where I need it to scroll.
You need to set a limit so that the table view cannot be larger than the view itself. Tableviews are built on UIScrollView and will handle scrolling on their own, you don't need to try to size it manually. The reason the table view bounces but doesn't scroll is because it is extending below the bottom of the screen. It wont scroll because it has already scrolled to the bottom, you just can't see it because it's outside of the superview.
-(void) changeTVHeight: (float) height {
CGFloat limitedHeight = MIN(height, self.view.frame.size.height)
CGRect newFrame = CGRectMake(120, 80, 180, limitedHeight);
self.myTableView.frame = newFrame;
}
I use Tableview to show library songs and songs play on did select cell,
when song play mini player popup on bottom.
At that time last cell of table view is not show properly, some of its portion hide behind mini player so please suggest.
Thanks
Try to set contentInset
tableView.contentInset = UIEdgeInsetsMake(0, 0, 110, 0); values are - top, left, bottom, right // change as per your needs
OR
- (void)viewDidLayoutSubviews {
CGFloat navbarHeight = 44; // this is supposed to be 64 but you should dynamically calculate it or better use constraints
CGRect tempFrame = self.view.frame;
self.tableView.frame = CGRectMake(tempFrame.origin.x, tempFrame.origin.y, tempFrame.size.width, tempFrame.size.height - navbarHeight);
}
A quick fix can be to add a footer View to your tableview. make the footer view height the same as your mini player. customise the footer view to your needs or make the footer view background colour to clear so it will not be visible.
I have a grouped UITableView. I've implemented tableView(:titleForHeaderInSection:) and tableView(:titleForFooterInSection:). As I scroll a long table, I can see that the section headers and footers contain the expected data. When I get to the bottom of the table and I drag up to see the footer of the last section, it has the correct data, but when I release my finger, the footer scrolls back down past the bottom of the screen and out of view. The last cell of the last section is what appears at the bottom of the screen rather than the footer of the last section.
How to fix it?
There's the last section and its footer. My finger is still on the screen
When I release my finger, the final footer slides off the bottom of the screen.
You can fix scrolling content issue by considering one of the following methods.
Method 1: Natural way to fix your problem by setting up your tableView frame and its bottom constraint properly from your storyboard.
Updated:
Method 2: You can validate your tableView frame in viewDidLayoutSubviews or viewWillLayoutSubviews
override func viewDidLayoutSubviews() {
tableView.frame = CGRect(x: 0, y: 0, width: tableView.frame.width, height: tableView.frame.height - (tabBarController?.tabBar.frame.size.height)!)
}
Method 3: Setting up your tableView frame by adjusting scroll view insets.
override func viewDidLayoutSubviews() {
tableView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: (tabBarController?.tabBar.frame.size.height)!, right: 0)
}
I think it's blocked by the tabbar.
If using storyborad, reset the constraint of your tableView.
If not, you need to set the frame of your tableView correctly.
EDIT: I think I am narrowing down the issue here. I just found an easier way to recreate what I want. If you create a new project with a storyboard, drag on a tableview controller and drag on a Search Bar as a subview of the tableview. If you run this (with some dummy cells), and you scroll so about half of the search bar is showing, the search bar will snap to the top of the view. If you try this by dragging on a UIView instead of a Search Bar, this behavior no longer happens. How can I get this behavior with a UIView as a subview?
Hopefully this made it more clear, thanks guys.
I have a header in my plain style table view, I am implementing it like this:
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
return 51;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
SegmentedHeaderView *header = [[SegmentedHeaderView alloc]initWithFrame:CGRectMake(0, 0, 320, 51)];
return header;
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
CGFloat sectionHeaderHeight = 51;
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);
}
}
I want the header to scroll with the tableview and not stick to the top, and it does that correctly. However, when I scroll to the top, I want the header to work like the search bar header does in the messages app in iOS 7. In the messages app, when you are scrolling and you stop halfway through the search bar, either the search bar sticks to the top of the view, or the first cell sticks to the top of the view.
How can I implement this so when the user stops scrolling with part of the header view showing the header jumps up?
Edit: The behavior I want can also be seen with the search bar in the Mail app.
I am doing a slide menu using a UITableView and I have 2 options on the menu and I want to put a button at the bottom like in this image:
I try to do that add a tableFooterView like that.
UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 500, 320, 70)];
footerView.backgroundColor = [UIColor blackColor];
self.tableView.tableFooterView = footerView;
However, the view appears just after the second cell, but I want it at the bottom.
Thanks for help.
No you shouldn't add any empty cells, that's just hacky. If you really need the button to be at the bottom, you should use layoutSubviews to control the frame of the tableView and the footerView.
- (void)layoutSubviews
{
[super layoutSubviews];
self.tableView.frame = // top 80% of the screen
self.footerView.frame = // bottom 20% of the screen
}
You should know that every UITableViewCell has its height and footer is part of a UITableView and will appear at the bottom of a UITableView. If you want to make your UITableView look like what that image shows, you should make sure that your cells are high enough to make sure that your UITableView are high enough so that footer will appear at the bottom of UITableView
My suggestion is to add extra "empty" cell(I mean a cell with no content but has a height).
Add a Container View with View Controller from storyboard. You can use autoresizing to set the buttons on right place.