I am trying to add UIView *separatorView at the top of my collection views and table views which appears when the respective view's contentOffset.y > 0. I have made this work on my collection view by adding the view in viewDidLayoutSubviews and added it to self.view. Then by using some of the scroll view delegate methods I can make it show and hide. This works well but for the table view, if I use the same concept the separatorView scrolls with the table view. I am still adding the separatorView to self.view but it behaves differently than on the collection view. I don't want the separatorView to scroll. I want it to stay at the top. Am I correct in believing that the way to make this work for the table view is to subclass UIViewController and manage a table view within that along with the separatorView? Is there any way to make it work in my current UITableViewController subclass?
Basically, why does self.view scroll with the table view and not with the collection view? This seems inconsistent.
Thanks for the help.
Edit: Added picture to demonstrate idea.
self.view in a UITableViewController is a UITableView because the tableViewController overrides loadView on the view controller.
If you run the following code:
UITableViewController *tableViewController = [[UITableViewController alloc] init];
NSLog(#"%#",tableViewController.view);
You would get the following console output:
2014-05-13 21:19:02.823 Test[28681:60b] <UITableView: 0x109834200; frame = (0 20; 320 548); clipsToBounds = YES; autoresize = W+H; gestureRecognizers = <NSArray: 0x109548f00>; layer = <CALayer: 0x109548c40>; contentOffset: {0, 0}>
As you can see, self.view is not a UIView but instead it is the UITableView. Your separator view scrolls with the tableView because calling:
[self.view addSubview:separatorView];
is exactly the same as calling:
[self.tableView addSubview:separatorView];
If you wanted the separator to stay in a fixed position it would need to be added to the tableView/collectionView's superview and not the scrolling view itself.
The reason this is not happening in the UICollectionViewController is because self.view is actually a UICollectionViewControllerWrapperView object. This is a private class but judging by its name, I am assuming it is a UIView that wraps the UICollectionView.
The most sensible way to achieve what you want would be to use a custom container view (child view controller) that would be a UIViewController subclass containing your separator view and tableView/collectionView controller.
If you didn't want to add a child view controller then the other alternative would be to like you said, create a UIViewController subclass and add your separator view and tableView as a subview within the view controllers view however if you don't use a UITableViewController you lose bonus functionality like automatically adjusting the tableView's contentInsets for keyboard appearance etc.
Related
I have controller, call it ActionBarViewController (it inherits UIViewController). It has view, call it ActionBarView. That view has 2 subviews - contentView and actionBar. contentView frame equals to ActionBarView frame. actionBar frame is small frame at bottom part of ActionBarView frame.
I have one more controller - ListViewController (it inherits UITableViewController). I use it as childViewController with ActionBarViewController as parent. ListViewController's view (tableView) set as contentView of ActionBarView.
As result, i have UITableViewController that is childViewController. ParentViewController view has additional subview - action bar.
For correct table view scrolling i add/remove additional contentInsets of tableView on actionBar show/hide. But ListViewController add/remove contentInsets on his own when keyboard appeared/disappeared.
Finally, when keyboard shown tableView has complex bottom insets, keyboard insets and actionBar insets.
Questions:
Any body knows correct way to resolve this issue?
In which point UITableViewController change tableView insets? How to override it?
P.S.: All views and controllers creating programmatically.
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.
An UISplitViewContller's the master view controller (TAbleView) has 2 types of cell prototypes.The iOS's default one is working fine but my custom cell has layout problems so all the subviews are missing.
If I present the master view controller modally all the subviews in the cell are laid out correctly.That means it is not related with the constraints set inside the content view of the cell.
Is there something special that i need to care about?
Second cell's subview are missing.
How they should look like.
Edit:
If i debug the layoutSubviews, i see that coordinates of the labels are not right.
<UILabel: 0x7fa8f0de82f0; frame = (0 -21; 42 21); text = 'Subtitle';
opaque = NO; autoresize = RM+BM;
userInteractionEnabled = NO; layer = <_UILabelLayer: 0x7fa8f0de84a0>>
Constraints for the Subtitle Label
After playing around with constraints I figured out that the problem was related with the size classes.
You need to specify the constraints for Splitview's master in ANYxANY size class not anything else.Mine were added in Regular to Any.
I have tall tableView and I want to add view on it, I want that it too will be tall.
I add view with this code but it only in one view height - 568 points.
_backgroundSelectionView = [[UIView alloc]initWithFrame:self.tableView.bounds];
_backgroundSelectionView.backgroundColor = [UIColor colorWithWhite:0.95 alpha:1];
[self.tableView addSubview:_backgroundSelectionView];
How can I add view on all screen?
UITableViewController provides UITableView and you cannot add subviews to tableViewController on top of tableview easliy.
Instead of using UITableViewController change it to UIViewController(which will conforms to UITableViewDataSource and UITableViewDelegate), with this you can add views(tableView and any other subView) on main UIView of UIViewController.
Have a storyboard w/ a TableViewController that has a grouped table view. In the first section, I want the cells' width to be smaller than full-screen. To accomplish the latter, I have a custom UITableViewCell class w/ the following method:
- (void)setFrame:(CGRect)frame {
frame.origin.x += NAME_TABLE_VIEW_INSET;
frame.size.width -= NAME_TABLE_VIEW_INSET;
[super setFrame:frame];
}
That works fine. The issue I have is that a UITextField subview that I dragged into the storyboard cell does not adjust its width automatically to the new cell frame size.
I've tried sub-classing UITextField and ensuring that the autoResizingMask is set properly, and I've tried using [super layoutSubviews] in the setFrame method above. None of these approaches works.
Any suggestions on how I can get the text field to adjust its width automatically while still using this storyboard approach?
The solution for this was to add a separate table view in the header of the tableView provided by the table view controller. Specifically, add a view object to the top of the table view in IB; add a table view as a subview into that view; change the width of the new table view's cells.
Note that this problem exists only because I wanted to use a table view controller, which defaults each cell to the width of the screen. A view controller could have been used, but then you cannot add a table view w/ static cells as a subview.