如何以编程方式创建UIScrollView?

编程入门 行业动态 更新时间:2024-10-09 14:24:53
本文介绍了如何以编程方式创建UIScrollView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

好吧,所以这里的关键是我根本不使用IB,因为我正在使用的View是以编程方式创建的。 UIView 覆盖了屏幕的下半部分,上面有一串按钮。但是,我想在 UIView 中添加更多按钮,而不会更大。为此,我想在视图中创建一个 UIScrollView ,这样我就可以在屏幕上添加更多按钮,以便用户可以滚动到它们。我认为它是如何工作的。

Alright, so the key here is I'm not using IB at all, because the View I'm working with is created programmatically. The UIView covers the lower half the screen, and has a bunch of buttons on it. However, I want to add more buttons to the UIView, without making it any larger. To do so, I want to make a UIScrollView inside the view, which will allow me to add more buttons off screen so the user can scroll to them. I think that's how it works.

self.manaView = [[[UIView alloc] initWithFrame:frame] autorelease]; self.manaView.backgroundColor = [UIColor purpleColor]; UIScrollView *scroll = [UIScrollView alloc]; scroll.contentSize = CGSizeMake(320, 400); scroll.showsHorizontalScrollIndicator = YES; [self.manaView addSubview:scroll];

代码的第一部分支持我的 UIView ,效果很好,但我无法弄清楚如何以编程方式制作 UIScrollView 并将其添加到视图中,然后将按钮添加到该视图中。

The first part of the code iniates my UIView, which works great, but I can't figure out how to make the UIScrollView programmatically and add it to the view, and then add the buttons to it.

UIButton *ret2 = [UIButton buttonWithType:UIButtonTypeRoundedRect]; ret2.tag = 102; ret2.frame = CGRectMake(255, 5, 60, 50); [ret2 setTitle:@"Return" forState:UIControlStateNormal]; [ret2 addTarget:self action:@selector(flipAction:) forControlEvents:UIControlEventTouchUpInside]; [scroll addSubview:ret2];

当我这样做时,按钮就会从屏幕上消失。那么我该怎么做呢?谢谢你的帮助!

When I did that, the button simply disappeared off my screen. So How do I do this correctly? Thank you for your help!

推荐答案

而不是:

UIScrollView *scroll = [UIScrollView alloc];

执行此操作(将帧设置为您希望滚动视图的大小):

do this (setting the frame to however big you want the scroll view to be):

UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:...];

更多推荐

如何以编程方式创建UIScrollView?

本文发布于:2023-11-28 11:13:57,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1642171.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:方式   UIScrollView

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!