IOS: adding a subview with a fixed position on the screen

How to fix the position of subviews on the screen (especially in UIScrollView and UITableView)? I think in the storyboard

[self.view addSubview:aSubView]; 

does not work any more.

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]; 
+4
source share
4 answers

As others have noted, this would be a little easier if you hadn't used the UITableViewController , but it's not that difficult.

UITableView is a subclass of UIScrollView , so the table view delegate (your instance of UITableViewController in this case) will also receive calls to UIScrollViewDelegate methods. All you have to do is implement a method that is called every time the scroll shift changes and the frame of your "fixed" view is set.

Something like that:

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

Replace 20 with how many points you want them to be on top of the table. You still add self.fixedView as a sublayer of self.view , it will just make sure that it looks like a fixed position above the table view.


Edit: with the code you submitted, I assume your example 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; } 
+17
source

Can you just add your subview to the window, for example:

 [self.view.window addSubview:mySubView]; 

This works for me. I added a representation of pixel position information in a dynamic table view.

+1
source

If it is a simple view controller containing a table view, [self.view addSubview: aSubView] should work. But if its a table view controller, it will not work.

0
source

This can be done by moving the sub (your) view from the UIScrollView to the super scroll view.

Here is a simple example:
Set / Set the button above the scroll (not inside the scroll), as shown here in this picture. And also set the limitations of the buttons (position) in relation to the super-view of your scroll.

enter image description here

Here's a link. A snapshot of the hierarchy of the position of each view on top of each other.

enter image description here

0
source

All Articles