iOS: Add subview with a fix position on screen

2020-02-26 12:10发布

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];

4条回答
Lonely孤独者°
2楼-- · 2020-02-26 12:38

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

查看更多
Juvenile、少年°
3楼-- · 2020-02-26 12:46

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.

查看更多
我欲成王,谁敢阻挡
4楼-- · 2020-02-26 12:47

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.

查看更多
啃猪蹄的小仙女
5楼-- · 2020-02-26 12:48

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;
}
查看更多
登录 后发表回答