如何让 UIView 覆盖 UIScrollView 作为页脚
我有一个 UIScrollView
,其中包含需要可缩放的图像。我想要一个带有黑色(不透明)背景和白色文本的“页脚”。我想将其固定为页脚。它是不透明的,因此您可以看到其背后的图像。
我为滚动视图和页脚创建了一个包含 UIView 的内容。我可以让滚动视图小于应用程序框架,并在底部有一个页脚填充额外的空间,但显然我看不到页脚后面的图像。
我还尝试将 UIScrollView
和 UIView
(页脚)放入容器内并相应地定位它们,但在这种情况下,我什至看不到页脚。有什么想法吗?
到目前为止我得到的代码(在视图控制器的 viewDidLoad 中执行):
CGRect appFrame = [[UIScreen mainScreen] applicationFrame];
UIView* view = [[UIView alloc] initWithFrame:appFrame];
view.backgroundColor = [UIColor blackColor];
CGRect scrollViewFrame = CGRectMake(0.0, 0.0, view.frame.size.width, view.frame.size.height - 100);
UIScrollView* scrollView = [[UIScrollView alloc] initWithFrame:scrollViewFrame];
[scrollView setCanCancelContentTouches:NO];
scrollView.clipsToBounds = YES;
scrollView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
[scrollView addSubview:imageView];
[scrollView setContentSize:CGSizeMake(imageView.frame.size.width, imageView.frame.size.height)];
scrollView.minimumZoomScale = 0.7;
scrollView.maximumZoomScale = 5;
scrollView.delegate = self;
[scrollView setScrollEnabled:YES];
UIView* textView = [[UIView alloc] initWithFrame:CGRectMake(0.0, scrollViewFrame.size.height, appFrame.size.width, 100)];
// Red for testing purposes
textView.backgroundColor = [UIColor colorWithRed:1 green:0 blue:0 alpha:0.5];
[view addSubview:scrollView];
[view addSubview:textView];
self.view = view;
I have a UIScrollView
with an image that needs to be scalable. I want to have a "footer" with a black (opaque) background and white text. I wanted to have it be fixed as a footer. It will be opaque so you can see the image behind it.
I created a containing UIView
for the scrollview and footer. I can get the scrollview to be smaller than the app frame and have a footer at the bottom filling in the extra space, but obviously I can't see the image behind the footer.
I also tried putting the UIScrollView
and UIView
(footer) inside the container and positioning them accordingly, but in this case I can't even see the footer. Any ideas?
Code I've gotten so far (executed in viewDidLoad
of view controller):
CGRect appFrame = [[UIScreen mainScreen] applicationFrame];
UIView* view = [[UIView alloc] initWithFrame:appFrame];
view.backgroundColor = [UIColor blackColor];
CGRect scrollViewFrame = CGRectMake(0.0, 0.0, view.frame.size.width, view.frame.size.height - 100);
UIScrollView* scrollView = [[UIScrollView alloc] initWithFrame:scrollViewFrame];
[scrollView setCanCancelContentTouches:NO];
scrollView.clipsToBounds = YES;
scrollView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
[scrollView addSubview:imageView];
[scrollView setContentSize:CGSizeMake(imageView.frame.size.width, imageView.frame.size.height)];
scrollView.minimumZoomScale = 0.7;
scrollView.maximumZoomScale = 5;
scrollView.delegate = self;
[scrollView setScrollEnabled:YES];
UIView* textView = [[UIView alloc] initWithFrame:CGRectMake(0.0, scrollViewFrame.size.height, appFrame.size.width, 100)];
// Red for testing purposes
textView.backgroundColor = [UIColor colorWithRed:1 green:0 blue:0 alpha:0.5];
[view addSubview:scrollView];
[view addSubview:textView];
self.view = view;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我假设“图像”意味着滚动视图中有一个 UIImage,并且您希望通过半透明的页脚视图看到它。
在这种情况下,您不应将视图放在彼此下方,而应将页脚视图放在滚动视图的顶部。您可以通过调用
bringSubviewToFront
或使用[containerView insertSubview:footer atIndex:0];
而不是addSubview:
来确保它位于顶部。另一个警告可能是您将视图称为
view
。对于哪个视图可能存在一些不可预测的行为,因此也许最好将其称为其他名称,然后将其分配给 self.view。I assume that with "image" you mean that there is an
UIImage
in the scroll view, and that you want to see it through a half transparent footer view.In this case you should not put the views beneath each other, but the footer view on top of the scroll view. You can insure it is on top by calling
bringSubviewToFront
or using[containerView insertSubview:footer atIndex:0];
instead ofaddSubview:
.Another caveat might be that you call your view
view
. There might be some unpredictable behavior about which view it is, so perhaps it is better to call it something else and then assign it toself.view
.我有很多 UIScrollViews,在底部或顶部有一个小的非滚动视图。在大多数情况下,我没有任何重叠,它们只是并排坐着。我在 InterfaceBuilder 中创建它们,并将它们分配给 IBOutlet。
I have plenty of UIScrollViews with a small non-scroll view at the bottom or the top. In most cases I don't have any overlap, and they just sit side-by-side. I create them all in InterfaceBuilder, and assign them to IBOutlets.