UIScrollView问题,只在一侧滚动

时间:2011-09-21 05:50:04

标签: iphone uiscrollview

我对编程很新,我已经找了很长时间的答案了。有一些关于它的帖子,但没有解决我的问题。我从笔尖得到一个UIScrollView视图,一切都很好,内容长度很好,滚动工作,但它只是在左侧滚动,如果我尝试在右侧滚动它不滚动..

这是代码,

- (void)viewDidLoad { 

    [super viewDidLoad]; 
    NSString *descriptionString = _currentBook.description; 
    CGSize stringSize = [descriptionString sizeWithFont:[UIFont boldSystemFontOfSize:16] constrainedToSize:CGSizeMake(387, 9999) lineBreakMode:UILineBreakModeWordWrap]; 
    _authorLabel.text = _currentBook.author; 
    _titleLabel.text = _currentBook.title; 
    _descriptionLabel.text = [NSString stringWithFormat:@"Description: %@",_currentBook.description]; 
    [(UIScrollView *)self.view setContentSize:CGSizeMake(387, stringSize.height +50)];
 }

提前致谢!

3 个答案:

答案 0 :(得分:1)

由于我们无法看到您的nib文件,因此很难理解该问题,但最好将scrollView放在视图控制器的视图顶部,并将其连接到视图控制器中的IBOutlet。

为了找到问题,我会删除用于测试目的的文本字段(我认为受约束的9999可能是一个问题,但我不确定)然后打印并发布scrollView的框架和内容大小我打赌你会看到uiscrollview框架的一些问题。

谢谢,

答案 1 :(得分:0)

好的,在复制粘贴并运行一些测试之后,我发现了问题。

问题在于问题的措辞,问题不在于“滚动在右侧不起作用”(如:在屏幕右侧上下移动手指而不触发)滚动),问题是内容,标签本身超出了界限,在scrollView的可见区域之外,右侧不可见。

首先,你应该注意到iPhone的分辨率是320x480(Retina的640x960),所以你实际上必须使用更小的宽度(使用387宽度会使它超出界限)。

其次,考虑到标签本身的x位置也会影响可见文本的数量。考虑到这一点,更通用的代码将是:

- (void)viewDidLoad 
{
    [super viewDidLoad];

    // This number represents the total width of the label that will fit centered in
    // the scrollView area.
    CGFloat visibleWidth = self.view.frame.width - descriptionLabel.frame.origin.x*2;

    // Use the number above to get a more accurate size for the string.
    NSString *descriptionString = _currentBook.description; 
    CGSize stringSize = [descriptionString sizeWithFont:[UIFont boldSystemFontOfSize:16] constrainedToSize:CGSizeMake(visibleWidth, 9999) lineBreakMode:UILineBreakModeWordWrap];

    // Fill data (why so many underscores?)
    _authorLabel.text = _currentBook.author; 
    _titleLabel.text = _currentBook.title; 
    _descriptionLabel.text = [NSString stringWithFormat:@"Description: %@",    _currentBook.description]; 

    // Also, why didn't you resize the description label? I'm assuming that you forgot this.
    // (Make sure that the descriptionLabel number of lines is 0)
    CGRect frame = descriptionLabel.frame;
    descriptionLabel.frame.size = stringSize;

    // Now set the content size, since you're using the scrollView as the main view of the viewController,
    // I'll asume that it's using the whole screen, so I'm gonna use the whole width
    // of the screen (that's what UIScreen is for).
    [(UIScrollView *)self.view setContentSize:CGSizeMake([UIScreen mainScreen].bounds.size.width, stringSize.height +50)];
}

答案 2 :(得分:0)

最后我发现了我的问题,我已经以编程方式添加了一个uiview作为某个视图的子视图然后到这个视图我已经将我的滚动视图添加为子视图..然后它只会在该区域中滚动我的UIView。这样做是无稽之谈,仍然缺乏知识.. 谢谢大家的帮助!

相关问题