Anchor a view at the bottom of a UITableViewController - ios

I'm building a view which is a UITableView, however I want to anchor a "Write a comment" UITextField at the bottom of the screen. You can see an example of this on these screenshots from the "Secret" app - the "Write a comment (anonymously)" and "Post" button are anchored to the bottom of the screen regardless of whether you scroll the tableview items up or down.
What's the best way to achieve this?
Should I be embedding a UITableViewController into a UIViewController with the UIView anchored to the bottom of the screen and the tableview anchored to the top of this view?
Is this some kind of UITableView section footer?
Any advice greatly appreciated.

You could subclass UITableView and add a bottom view (not to confuse with a tableview's footerView). Since a UITableView is a subclass of UIScrollView, you can change its contentInsets so that the content of the tableview will still scroll above your bottom view.
tableView.contentInsets = UIEdgeInsetsMake(0, 0, 0, bottomViewHeight);
The next step would be to make the bottom view sticky, that is, floating with the bottom of the tableview. You can achieve this in multiple ways. Here are two suggestions:
1) Manipulating the frame directly
By conforming to UITableViewDelegate you automatically conform to UIScrollViewDelegate. You can see this by inspecting the protocol declaration in UITableView.h:
#protocol UITableViewDelegate<NSObject, UIScrollViewDelegate>
Then implement scrollViewDidScroll:(UIScrollView *)scrollView and change the y-offset of the bottomview to always position it at the bottom of the view. UIScrollView's contentOffset property is used to determine how far down the scrollview has scrolled. This method will be called every single time the scrollview scrolls, hence it will appear that the bottom view sticks to the bottom of the tableview.
2) Use auto layout
While still changing the contentInsets as above, you can achieve the sticky effect by using auto layout constraints instead. By pinning the bottom view to the edge of the scrollview, it will automatically create the sticky effect for you. This is by far my recommended approach, since it saves lines of code, while it uses the highest possible level of abstraction.
I use this category by Florian Kugler when implementing auto layout in code.
This technical note, however not strictly related to the issue, describes how to use auto layout with scrollviews.

I have in-app chat (like whatsapp) and I have the following structure:
UIViewController
\
- View
\
- UITableView
- UIView (with textfied)
I thinks this is the best approach as you don't mix table data with anything else, and you don't have to juggle with sections in code

if you requirement is to show only one picture related information at once in screen means please follow the process it will help you.
first take a view controller then take an image view and on it the buttons you needed add them on image view only below image view take a tableview(if there are comments show tableview. if no comments available show a label as"be first to comment").
Hope this will help

Related

One section of the view controller is still and one section horizontally scrollable

I would like to construct a view controller where one section of the view controller would be still and one section scrollable.
Both sections have headers where as well, one is still and one is moving along with the content in the section.
I do not want the cells in the section to be scrolled separately. All cells should move at the same time along with the header.
I have added an image to make my point little more clearer.
Use UICollection View for both view and disable scrolling for one view and enable scrolling for another view
you can probably add to your UIViewController view a UITableView on the left with fixed size (for example 150px) and vertical scroll disabled and a UICollectionView with horizontal flow and ,if needed a, with custom UICollectionViewLayout (but i think that you just need the classic UICollectionViewFlowLayout) for the right part that fits the remaining space.
Here you can find the component's documentation:
https://developer.apple.com/reference/uikit/uicollectionview
https://developer.apple.com/reference/uikit/uitableview

how to make a space at the bottom of a UITableView in a UITableViewController

I want to add a footer to the UITableView, there are two ways, either footer for the section, or footer for the UITableView. Both don't work for me because I want to footer always at the bottom of the screen. In other words, floating footer.
That's why after searching, people suggest to but the footer under the UITableView. I would need to resize the UITableView and add the footer through IB under it. That makes sense and that's what I am asking for.
Kindly I have a specific problem and It's not about footer at all
My problem is that I couldn't create constrains for the UITableView that locates inside UITableViewContoller, whenever I click Ctrl + drag, I don't see the options that I would normally see.
Is that because I'm working with UITableViewController?
This is a screenshot of my hirechy
Again I would like to resay that my problem is not about footers, my problem is about how to resize the UITableView in a UITableViewController
I created a sample project with a fixed footer. You can download it here. What I have added is:
ContainerView 100%
- TableView 90% of the ContainerView and constraints to the ContainerView
- View 10% of the ContainerView and constraints to the ContainerView
You shouldn't resize the UITableView of an UITableViewController object : your tableview is in fact your main view.
If you want to put a empty space that can't be scrolled below an UITableView, you should use an UIViewController instead.
Add an UITableView and an UIView to this UIViewController object.
Set the left, right, and bottom constraints aligned to its superview for the UIView, and fix its height with a fourth constraint. Add the left, right and top constraints aligned to its superview for the UITableView, and set the vertical space between the view and the tableview to 0 with a constraint.
Yes, you are correct. You cannot add a footer on a UITableViewController. For this I recommend using a UIViewController.
Follow these steps if you want to add a footer:
create UIViewController
Add footer to the bottom and add constraints to the bottom border.
Add UITableView to the UIView, and add constraints to the footer.
Also for the controller, just create two classes within the same file, and make one of them as a UITableViewController and the footer as regular class within the file.
If you just want space you should just use UIViewController. Add UITableView inside that, and put constraints on that.
This is one approach, and there maybe many other. Please let me know if I can help you any further.

How to setup a UIScrollView to arrange content dynamically?

I am trying to create a user profile screen. The screen will have lots of information about a user. It will look similar to this AirB
From the looks of it, it seems to be some sort of scrollView? or is it a tableView or a collectionView? (because it seems that there are some rows in there i think)
Does any one here know how this type of view can be accomplished or can be setup?
EDIT two answers below says to use a UITableview, and 2 other answers says to avoid it. Are there any benefits/disadvantages to using either?
Use only one child UIView as i will call it ContainerView inside UIScrollView and then place your child views inside that ContainerView. Use constraint to trailing and leading and top and bottom of that ContainerView to UIScrollView. and use constraint for placing child views to viewController.view not to ContainerView to then iOS will find where child views have to be.
Good Tutorial to find out how to use constraint on scrollView
Apple Technical Note
Do Not use tableview in this case, this content is difficult to maintain in a table view, really, use a scrollview, as for how, a simple googling for "swift uiscrollview tutorial"
Tableview is the best solution for this kind of scrolling views but in the screen you have mapview if it updates every moment i.e. updates with current location then it is not recommended to use tableview.
In that case you can use scrollview with custom views add subview changing with parameters.
In one of my app i did same thing using Tableview. And for that tableview i created a custom cell which have such a big height and i added all the images,map,details of user in that. And it work similar to what you want.
If you want help reply to my answer

Twitter iOS Profile Page Configuration

I'm trying to structure a page similar to Twitter's profile page. It looks like they are using a basic UITableView. The top profile is just the tableHeaderView. The tabs are a UISegmentedControl inside the SectionRowHeader so that it sticks to the top when scrolled. And the tweets are just the cells inside that section. And if you switch tabs, it seems like they are simply changing the underlying data and reloading the table.
However if you play with the page, you'll notice that changing tabs keeps the previous scroll position. And if you scroll back to the top on one tab, then of course the other tab has to be scrolled to the top otherwise it'll be scrolled underneath the header. Also, if you look closely the bottom part (the tweets) has its own scrollbar once you start scrolling. So it seems like that is its own tableview. So would that be a new tableview inside the cell of the main page? And if that's what they're doing, then the scrolling is seamless between them. You're able to scroll the bottom part up and only once the tabs are pinned to the top do the tweets scroll underneath it.
I'm trying to build a similar structured page but keep running into issues. Ideally I would also like to have the logic of those sub tabs broken out into separate view controllers for reuse but at this point I would just like to get this working.
Anyone have any idea of the structure they're using to setup a page like this?
I'm 99% certain they are simply adjusting the scroll indicator insets as you scroll view the table:
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
CGFloat const visibleHeaderHeight = 0.0; // your logic here
UIEdgeInsets const insets = UIEdgeInsetsMake(visibleHeaderHeight, 0.0, 0.0, 0.0);
self.scrollView. scrollIndicatorInsets = insets;
}
Also I'm pretty sure they are using a grouped style table view due to the sectioning, so you would need to build the sticky header yourself. You can also do that in -scrollViewDidScroll: though.
If you want to break the tabs into separate view controllers, I would recommend the following setup:
have a container view controller managing the header view containing the tabs
use UIViewController containment to plug in the active view controller
subscribe to the active view controller's scroll view's contentOffset and adjust the scroll indicator inset and sticky header y position accordingly
I tried to explain a bit here.
After a long long investigation that is how i achieve the twitter
profile behaviour.
UnderlayScrollView
MasterScrollView
Header ViewController
Bottom ViewController
PagerTabItems [CollectionView]
UIPagerController or any other horizontal scroll (Parchment, XLPagerTabStrip).
UnderlayScrollView is responsible of controlling the scroll gesture.
its contentoffset is used to adjust inner scroll contentOffsets.
Contentsize of the underlaying scroll is same as the masterscroll's
contentsize.
See the source code on github for more.
click

Anchor a UIButton to the bottom of a UITableViewController's view

I have the following requirement. When a UITableViewController's view is displayed, there are a variable number of rows. Underneath the rows, a button should be displayed.
When the number of rows is small, the button should be anchored to the bottom of the view.
When the number of rows is larger, the delete button should be placed immediately after the last row.
In other words:
And not:
My best attempt at this so far has involved setting a tableFooterView and trying to update its height using the contentSize of the UITableView, but I am running into all sorts of problems. I might continue down this path and ask for some help, but first I want to know if anyone has alternative (better) solutions.
The result must play nicely with a double-sized status bar (during a call for example) and I am targeting iOS 6.0. I am not using interface builder.
One possible solution to achieve this effect might have to use two different solutions.
If the amount of rows means that the button will be off the screen then use the footerView like you have been doing.
If the amount of rows means that the button will not be off screen then
Add the button to the tableView
Implement - (void)scrollViewDidScroll:(UIScrollView *)scrollView and update the frame of the button to be offset from the bottom.
The offset from the bottom might follow some logic like this
yOffset = CGRectGetHeight(tableView.frame) - (CGRectGetHeight(button.frame) + somePadding)
yOffset += tableView.contentOffset.y
This would mean that the button still moves up and down with the scrolling but you don't have to mess with the footerView height
Keep both the table view and a button inside scroll view. Keep the button at the bottom of the scrollview. For proper scrolling to work you might want to set the scrollEnabled property of the scroll views. For more details on that check this up
Scrolling a UITableView inside a UIScrollView
EDIT:
yourView.autoresizingMask = UIViewAutoresizingFlexibleTopMargin ;
Check the accepted answer for this question for more details on implimenting struts and springs using code:
UIView autoresizingMask - Interface Builder to Code - Programmatically create struts and springs - Swift or Objective-C

Resources