在某些 iPhone 应用程序中,我看到一个按钮 float 在内容 View 上,例如。在应用程序 EyeEm 中。当用户滚动内容时,按钮保持在原处并且仍然是一个交互元素。
我该如何实现?
我的方法是:
- 创建包含内容的 View
- 在上面放一个按钮
- 但是如何让按钮 float 呢?
编辑:
float 似乎是默认行为。有趣的是,addSubview
和 insertSubview
在放置按钮时具有相同的行为...两者都 float 在内容上。
- (void)addOverlayButton {
UIButton *oButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[oButton addTarget:self
action:@selector(aMethod:)
forControlEvents:UIControlEventTouchDown];
[oButton setTitle:@"Show View" forState:UIControlStateNormal];
oButton.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
[self.view addSubview:oButton];
//[self.view insertSubview:oButton aboveSubview:_scrollView]; // same result as addSubview.
// Both solutions let the button float over the content.
}
最佳答案
使用 insertSubview:aboveSubview:
方法将 subview 添加到您的窗口的内容 View 中,并将您的按钮和 ScrollView 作为参数,但要小心:如果两个同级 View 都具有透明度,则生成的绘制行为是未定义的.
关于objective-c - 如何实现 float 在内容 View 上的叠加按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17040776/