iphone:uiscrollview不滚动?但内容设置..?

dac*_*dac 7 iphone scroll uiscrollview

嘿人,我有一些数据,并希望在导航堆栈内的新视图中显示它.

我用XIB文件创建了一些新的viewcontroller类.然后我编辑了XIB文件并放置了以下层次结构:

1 files owner
2 first responder
3 scrollview
3.1 view
3.1.1 label1
3.1.2 label2
3.1.3 label3
3.1.4 image4
Run Code Online (Sandbox Code Playgroud)

我安排了标签和图像.标签的内容可能不同,它们应该动态调整大小.我还在viewcontroller类中完成了IBOutlets并正确连接了所有内容.文件所有者的视图委托设置为查看..

然后我加载了viewcontroller,我在"viewDidLoad"中执行 - 方法如下:

- (void)viewDidLoad {
[super viewDidLoad];

self.currentPageURL.text = [self.offerItem objectForKey: @"page-url"];
self.currentPrice.text = [self.offerItem objectForKey: @"price"];
self.currentRank.text = [self.offerItem objectForKey: @"rank"];
self.currentName.text = [self.offerItem objectForKey: @"name"];
self.currentProducerName.text = [self.offerItem objectForKey: @"producer-name"];

self.currentDescription.text = [self.offerItem objectForKey: @"description"];

self.imageViewForImage.image = [helperClass resizeImage:[self.offerItem objectForKey: @"picture"] forSize:CGSizeMake(280.0, 280.0) ];

[theScrollview setScrollEnabled:YES];
[theScrollview setContentSize:CGSizeMake(320, 1200)];
[theScrollview flashScrollIndicators];

[theScrollview addSubview:theView];
Run Code Online (Sandbox Code Playgroud)

}

当我在我的模拟器中启动应用程序时,所有内容都会显示,但如果我尝试滚动,则没有任何反应.

我做错了什么?

Thi*_*res 20

Autolayout在视图加载后设置了一些约束,因此,选择在viewDidLoad中设置内容大小并放入viewDidAppear的代码,您的代码将不会被重写.

这对我有用.

-(void)viewDidAppear:(BOOL)animated{
  [self.scrollView setContentSize:CGSizeMake(320, 1000)];
}
Run Code Online (Sandbox Code Playgroud)

抱歉我的英语不好.


Joh*_*llo 11

确保在IB Document属性中取消选中 "Use Autolayout" .我有完全相同的问题,设置正确的contentSize和所有,但启用此功能后,始终禁用滚动.

使用Autolayout需要关闭

更新要使用Autolayout,请在viewDidAppear方法中显式设置scrollView内容的宽度和高度,如下所示:

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];

    // set the scrollview to the specified size
    CGRect screenRect = [[UIScreen mainScreen] bounds];
    [scrollView setContentSize:CGSizeMake(screenRect.size.width, 1100)];

    [scrollView setBounds:CGRectMake(0, 0, screenRect.size.width, screenRect.size.height)];

    [scrollView setScrollIndicatorInsets:UIEdgeInsetsFromString(@"{0,0,0,-1.0}")];

}
Run Code Online (Sandbox Code Playgroud)


Dee*_*olu 4

立即将框架更改theScrollView为 320x480,您应该会看到滚动。

  • 等一下?“3.1”处的“view”是由“self.view”引用的吗? (2认同)