Stick UITableView header view to top when creating header in storyboard
rdelmar's comment helped me understand the reasoning behind it not working. Here is what I did in practice to fix the problem:
I had to drag the header view in storyboard down by where the first responder is. You'll notice you can't actually drag the header view down on the view itself, you'll have to drag the element down from the document outline.
I also had to implement viewForHeaderInSection (returning the header view that I made an outlet) and heightForHeaderInSection.
Every time you want to edit the view via storyboard, you'll have to drag it back up to edit it, then drag it back down so it works correctly.
How to make UITable header stick to the top of the screen when using Storyboards?
I was able to do it (at least in iOS 6) like this, don't know why this thing works:)
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
if(self.headerManualView == nil) {
//allocate the view if it doesn't exist yet
headerManualView = [[UIView alloc] init];
//create the button
UITextField *txtField = [[UITextField alloc] initWithFrame:CGRectMake(10, 3, 250, 44)];
//the button should be as big as a table view cell
txtField.borderStyle = UITextBorderStyleRoundedRect;
//set action of the button
//[txtField addTarget:self action:@selector(removeAction:) forControlEvents:UIControlEventTouchUpInside];
//add the button to the view
[headerManualView addSubview:txtField];
}
//return the view for the footer
return headerManualView;
}
How do you produce a Storyboard header that doesn't scroll with the table beneath it and doesn't bounce on refresh?
It looks like you're on the right track...
Difficult to tell from the image you posted, but what you want to do is use a UIViewController
... NOT a UITableViewController
. Then you can add a view as your "header view" at the top, and add a UITableView
below that "header view."
Here's how it might be laid-out:
Edit - after comments...
Do NOT try to edit the XML of your Storyboard. Unless you know exactly what you're doing (including all of the quirks and intricacies of that XML), you're just asking for trouble.
You can copy/paste (or directly move) UI elements.
The approach I would take, starting with this Storyboard (a UITableViewController
with a cell Prototype):
Add a new UIViewController
to your Storyboard:
Add and constrain your "header" view:
Add and constrain a UITableView
:
Now drag the cell Prototype from the UITableViewController
to the table view in your new UIViewController
:
The result:
Now you can delete your original table view controller... assign your class to the new view controller... make sure you have all @IBOutlet
and @IBAction
connections set... re-connect any segues you need... etc.
Table Header Views in StoryBoards
It looks like one simply drags a control to the top of the table view. I didn't expect it to be that easy.
Before Drop
After Drop
UITableView header will not stick to the top of UITableView
What you're describing is the table view header. You don't add it to the table directly, but rather, you assign it to the table's tableHeaderView
property like this:
self.tableView.tableHeaderView = myHeaderView;
Or you can just drag and drop a view into the storyboard at the top of the table view above the first section.
Is it possible to have a fixed uitableview Header while using sections?
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:
Stick TableHeader Image always on top of the Screen?
You have to use the following in viewDidLoad()
:
topView = UIView(frame: CGRectMake(0, 0, 320, 300))
let testView = UIView(frame: CGRectMake(0, 0, 320, 200))
testView.backgroundColor = UIColor.clearColor()
tableView.tableHeaderView = testView
let img = UIImage(named: "bg")
topImageView = UIImageView(image: img)
topImageView?.frame = CGRectMake(0, 0, 320, 180)
topView.addSubview(topImageView!)
topImageView?.contentMode = UIViewContentMode.ScaleToFill
tableView.addSubview(topView!)
tableView.sendSubviewToBack(topView!)
UITableView with fixed section headers
The headers only remain fixed (floating) when the UITableViewStyle
property of the table is set to UITableViewStylePlain
.
If you have it set to UITableViewStyleGrouped
, the headers will scroll up with the cells (will not float).
Is it possible to disable floating headers in UITableView with UITableViewStylePlain?
You should be able to fake this by using a custom cell to do your header rows. These will then scroll like any other cell in the table view.
You just need to add some logic in your cellForRowAtIndexPath
to return the right cell type when it is a header row.
You'll probably have to manage your sections yourself though, i.e. have everything in one section and fake the headers. (You could also try returning a hidden view for the header view, but I don't know if that will work)
Related Topics
Get The Data from All Children in Firebase Using Swift
Map Object into 2D Array Swift for Tableview Sections
Filemanager and Urlsfordirectory Error in Swift 3 Xcode 8
Swift4: Callback Url Not Approved for This Client Application
Atos Does Not Symbolicate System Frameworks/Libraries Properly
How to Select Table View Row Selection with Custom Checkbox Button
Uiactivityviewcontroller Subject Not Working for Outlook
Draw Polyline Using Google Maps in Custom View with Swift 3
How to Set Requestserializer in Alamofire
Restkit Request Not Sending Parameters
Add a UIbutton as a Subview to a UItabbar
Custom Uibarbuttonitem with Bg Colour and Text Swift 3
How to Integrate .Proto Files in Xcode Compilation
How to Override Convenience Init in Uialertcontroller for Swift
Xcode Error Extra Info About Plist: Acl=<Not Found>
Translate Just 4 Lines of Code from Objective C to Swift (Pointers)