fil*_*lou 4 xcode position uiview ios
如何在屏幕上修复子视图位置(尤其是在UIScrollView和UITableView中)?我想在故事板中
[self.view addSubview:aSubView];
Run Code Online (Sandbox Code Playgroud)
不再起作用了.
有任何想法吗?
编辑#1:我使用的是UITableViewController,而不是简单的UITableView.
编辑#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];
Run Code Online (Sandbox Code Playgroud)
Fil*_*lic 17
正如其他人所指出的那样,如果你不使用a,这会更容易UITableViewController,但不管怎么说都不是那么难.
UITableView是子类UIScrollView,因此表视图的委托(UITableViewController在这种情况下是您的实例)也将接收UIScrollViewDelegate方法调用.您所要做的就是实现每次滚动偏移更改时调用的方法,并调整"固定"视图的框架.
像这样的东西:
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
CGRect fixedFrame = self.fixedView.frame;
fixedFrame.origin.y = 20 + scrollView.contentOffset.y;
self.fixedView.frame = fixedFrame;
}
Run Code Online (Sandbox Code Playgroud)
将表格视图顶部所需的点数替换为20.您仍然添加self.fixedView作为子视图self.view,这将确保它看起来像在表视图上方的固定位置.
编辑:你发布的代码,我猜你的版本应该是这样的:
- (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;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
22840 次 |
| 最近记录: |