iOS:在屏幕上添加具有固定位置的子视图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13663230/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 21:23:31  来源:igfitidea点击:

iOS: Add subview with a fix position on screen

iosxcodeuiviewposition

提问by filou

How do I fix a subviews position on screen (especially in UIScrollView and UITableView)? I think in storyboard

如何修复屏幕上的子视图位置(尤其是在 UIScrollView 和 UITableView 中)?我在故事板中思考

[self.view addSubview:aSubView];

does not work anymore.

不工作了。

Any ideas?

有任何想法吗?

EDIT #1: I am using a UITableViewController, not a simple UITableView.

编辑 #1:我使用的是 UITableViewController,而不是简单的 UITableView。

EDIT #2:

编辑#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];

回答by Filip Radelic

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

正如其他人指出的那样,如果您不使用 a ,这会更容易一些UITableViewController,但无论如何都没有那么难。

UITableViewis a subclass of UIScrollView, so table view's delegate (your UITableViewControllerinstance in this case) will also receive UIScrollViewDelegatemethod 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.

UITableView是 的子类UIScrollView,因此表视图的委托(UITableViewController在本例中为您的实例)也将接收UIScrollViewDelegate方法调用。您所要做的就是实现每次滚动偏移更改时调用的方法并调整“固定”视图的框架。

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.fixedViewas a subliew of self.view, this will just make sure it looks like it's in a fixed position above table view.

将 20 替换为您希望它位于表格视图顶部的点数。您仍然添加self.fixedView为的 subliew self.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;
}

回答by Mavlarn

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.

这个对我有用。我在动态表视图中添加了一个像素位置信息视图。

回答by Krunal

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

可以通过将子(您的)视图从 UIScrollView 移动到滚动视图的超级视图来完成。

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

在此处输入图片说明

回答by Kiran Kumar

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.

如果它是一个包含表视图的简单视图控制器,则 [self.view addSubview:aSubView] 应该可以工作。但是如果它是一个表视图控制器,它就行不通了。