iphone - 滚动条不显示

标签 iphone ios objective-c

我是 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 英尺的房间

UIScrollView Documentation

关于iphone - 滚动条不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19097279/

相关文章:

iphone - 在 UITabBarController 中滑动 UITabBarItems

ios - 为什么我在使用 XCode 时不能拥有名为 'retain' 的属性?

objective-c - 如何使大纲 View 像表格 View 一样可重新排序?

ios - 如何正确使用OperationQueue?

ios - 将静态库添加到 Xcode 项目时出现重复符号

iphone - -[MyClassName copyWithZone :] unrecognized selector sent to instance

ios - 在 iOS 应用程序中创建、读取和写入纯文本文件

iphone - 在 iOS 7 上读取 iPhone 的通话记录

iphone - 如何以编程方式为 iPhone 和 iPad 播放闹钟?

ios - GLKBaseEffect 未加载纹理(纹理在对象上显示为黑色)