开发者

how to add a uiscrollview in IB

how is it possible to add an image to be set inside an uiscrollview using just inte开发者_运维问答rface builder and no code. i tried simply adding an image into the scrollview and it didnt work. i know this is a really simple answer however i didnt find any IB related help on achieving this


You can add the image to the scroll view in Interface Builder, but in order for it to scroll (even if the image is larger than the scroll view) you need to manually set the scroll view's contentSize property in code, as I am not aware of any way to set this property in Interface Builder. In your view controller's viewDidLoad method, you can add something like:

scrollView.contentSize = imageView.frame.size;

Only one line of code, shouldn't be too much trouble.


Alternatively as other people have suggested you can create a custom subclass of UIScrollView. Then overided initWithCoder:

- (id)initWithCoder:(NSCoder *)coder 
{
self = [super initWithCoder:coder];
if (self) {

    if ([self.subviews count] > 0) {
        UIView* subview = [self.subviews objectAtIndex:0];
        self.contentSize = subview.frame.size;

        self.alwaysBounceHorizontal = NO;
        self.alwaysBounceVertical = YES;
        self.showsHorizontalScrollIndicator = NO;
        self.scrollEnabled = YES;
    }

}
return self;
}

Then set the class of your scroll view in IB to your custom subclass. This will mean the content size is set automatically.

This only works if you have one subview in the scroll view. Otherwise add create a new 'container view', put everything in it, then add this as the only subview to the scroll view.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜