Reputation: 4843
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.
Upvotes: 1
Views: 3791
Reputation: 4191
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:
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.
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.
Upvotes: 2
Reputation: 1097
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
Upvotes: 0
Reputation: 4012
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
Upvotes: 1