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)];
 }

提前感谢!

很难理解这个问题,因为我们看不到你的nib文件,但更好的做法是将scrollView放在视图控制器的视图顶部,并将其连接到视图控制器中的IBOutlet。

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

谢谢,

好的,经过复制粘贴和运行一些测试,我发现了问题。

问题在于问题的措辞,你的问题不是"滚动在右侧不起作用"(如:你在屏幕的右侧上下移动手指而不触发滚动),问题是内容,标签本身正在出界,在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)];
}

最后我发现了我的问题,我已经添加了一个uiview编程作为子视图到一些视图,然后到这个视图我已经添加了我的滚动视图作为子视图。然后它只会在我的UIView区域滚动。这样做是毫无意义的,仍然缺乏知识。谢谢大家的帮助!

最新更新