How do I fix a subviews position on screen (especially in UIScrollView and UITableView)? I think in storyboard
[self.view addSubview:aSubView];
does not work anymore.
Any ideas?
EDIT #1: I am using a UITableViewController, not a simple UITableView.
EDIT #2:
CGRect fixedFrame = self.menuViewRelative.frame;
fixedFrame.origin.y = 0 + scrollView.contentOffset.y;
self.menuViewRelative.frame = fixedFrame;
menuViewRelative = [[UIView alloc] init];
menuViewRelative.backgroundColor = [UIColor grayColor];
menuViewRelative.frame = CGRectMake(0.0, 0.0, 320.0, 50.0);
[self.view addSubview:self.menuViewRelative];
As others noted, this would be a bit easier if you didn’t use a
UITableViewController, but it’s not that hard anyway.UITableViewis a subclass ofUIScrollView, so table view’s delegate (yourUITableViewControllerinstance in this case) will also receiveUIScrollViewDelegatemethod calls. All you have to do is implement the method that gets called every time scroll offset changes and adjust the frame of your “fixed” view.Something like this:
Replace 20 by how many points you want it to be from top of the table view. You still add
self.fixedViewas a subliew ofself.view, this will just make sure it looks like it’s in a fixed position above table view.Edit: with the code you posted, I’m guessing your verion should look like this: