我是 ios 编程新手。
我希望用户能够滚动屏幕,因此我初始化了 UIScrollView 并将其他 View 添加到 UIScrollView 实例中。
但是,我无法滚动屏幕,也没有看到滚动条。
这是我写的代码。 该 View Controller 继承自 UIViewController。
这有什么问题吗?
请帮帮我。 非常感谢!!
- (void)viewDidLoad
{
[super viewDidLoad];
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:self.view.frame];
scrollView.contentSize = self.view.frame.size;
[self.view addSubview:scrollView];
UIImage *img = [UIImage imageNamed:@"avatar.png"];
UIImageView *profileImg = [[UIImageView alloc] initWithImage:img];
CGPoint newPoint = self.view.center;
newPoint.y = 300;
profileImg.center = newPoint;
[scrollView addSubview:profileImg];
}
最佳答案
您需要设置内容大小并启用滚动
[scrollView setFrame:self.view.bounds]; // not needed to be able to scroll
[scrollView setScrollEnabled:YES]; // needed to be able to scroll, defaults to YES
[scrollView setContentSize:CGSizeMake(320, 500)]; // the important part that is needed to be able to scroll
320 和 500 是滚动到的最大距离,这不是实际的滚动距离,而是将显示的像素的宽度和高度,您可以将 ScrollView 视为一个窗口,这些值就是世界外面
这就是你的主要错误所在, ScrollView 的宽度和高度与上下文大小的宽度和高度相同,因此;就像您正透过 1 英尺乘 1 英尺的 window 看到一个只有 1 英尺乘 1 英尺的房间
关于iphone - 滚动条不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19097279/