4

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];
Krunal
  • 77,632
  • 48
  • 245
  • 261
filou
  • 1,609
  • 5
  • 26
  • 53
  • please flesh out your question a bit.... I dont get what you want to do. – Daij-Djan Dec 01 '12 at 20:07
  • in ios 6 u can use constraints in xib – Saurabh Passolia Dec 01 '12 at 20:08
  • I want to fix the position of a subview (UIView) so it is not scrolling with the content of a UITableView. The subview should stay fixed on screen.. – filou Dec 01 '12 at 20:10
  • You add the view that should not scroll as a sibling of the scrolling view. Suppose your scrollview or tableview is a child of view/window `XY`, simply add your fixed view as a subview of XY as well. – Till Dec 01 '12 at 22:57
  • @Till: Sorry, I forgot to say that it's a UITableViewController :S Excuse me. Any idea anyway? – filou Dec 02 '12 at 16:54
  • What prevents you from using a UITableView within a regular UIViewController? – Till Dec 02 '12 at 16:56
  • @Till: Static Cells are only accepted in a UITableViewController. – filou Dec 02 '12 at 17:08

4 Answers4

17

As others noted, this would be a bit easier if you didn't use a UITableViewController, but it's not that hard anyway.

UITableView is a subclass of UIScrollView, so table view's delegate (your UITableViewController instance in this case) will also receive UIScrollViewDelegate method 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:

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGRect fixedFrame = self.fixedView.frame;
    fixedFrame.origin.y = 20 + scrollView.contentOffset.y;
    self.fixedView.frame = fixedFrame;
}

Replace 20 by how many points you want it to be from top of the table view. You still add self.fixedView as a subliew of self.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:

- (void)viewDidLoad {
    menuViewRelative = [[UIView alloc] init];
    menuViewRelative.backgroundColor = [UIColor grayColor];
    menuViewRelative.frame = CGRectMake(0.0, 0.0, 320.0, 50.0);

    [self.view addSubview:self.menuViewRelative];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
    CGRect fixedFrame = self.menuViewRelative.frame;
    fixedFrame.origin.y = 0 + scrollView.contentOffset.y;
    self.menuViewRelative.frame = fixedFrame;
}
Filip Radelic
  • 26,607
  • 8
  • 71
  • 97
  • Tried this way, but it does not seem to work (please have a look at the code in my question above). – filou Dec 02 '12 at 19:04
  • Where did you put that code? You should not have those other 4 lines in this method, they should stay in your viewDidLoad or where ever you had them. The method I posted *does* work, you just have to use it like I described. – Filip Radelic Dec 02 '12 at 19:10
  • Check out the edit to my answer, if you put it that way it will work. – Filip Radelic Dec 02 '12 at 19:12
  • I use this 4 lines in the same method, because I need to know when y < -50 to let the subview appear :S – filou Dec 02 '12 at 19:18
  • Well you are doing it wrong. I answered your question, you should accept the right answer and if you have any additional questions ask them. I can't read your mind. – Filip Radelic Dec 02 '12 at 19:20
  • ok, it's working. But the separator lines scoll 'over' the frame. Is there a way to bring the frame in front of the TableView? I can't accept your answer if it's not working perfectly.. – filou Dec 02 '12 at 19:36
  • That should not be happening. Try `[self.view bringSubviewToFront:self.menuViewRelative];` inside `scrollViewDidScroll`. – Filip Radelic Dec 02 '12 at 19:56
3

Can you just add your subview into the window, like:

[self.view.window addSubview:mySubView];

It works for me. I added a pix position info view in a dynamic table view.

Mavlarn
  • 3,807
  • 2
  • 37
  • 57
0

If its a simple view controller which contains a table view, [self.view addSubview:aSubView] should work. But if its a table view controller, it wont work.

Kiran Kumar
  • 1,192
  • 8
  • 10
  • Absolutely right. It is a UITableViewController.. but how can I fix the position of the subview anyway? – filou Dec 02 '12 at 16:53
  • You cant. Use a simple UIViewController and add a UITableView as a sub view to it. Then follow the regular way of binding data source, populating data etc. Once this is done, you can use [self.view addSubview:view] – Kiran Kumar Dec 02 '12 at 19:22
  • There could be work around to get a fixed positioned item inside a table view. But it is not advisable. Rather, fixing your view controller is the right way of doing it. – Kiran Kumar Dec 02 '12 at 19:26
0

It can be done, by moving sub (your) view from UIScrollView to super view of scrollview.

Here is simple example:
Place/set your button over scroll view (not inside scroll view) as shown here in this snapshot. And also set button constraints (position) with respect to super view of your scrollview.

enter image description here

Here is ref. snapshot of hierarchy of position of each view over each-other.

enter image description here

Krunal
  • 77,632
  • 48
  • 245
  • 261