Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Don't subclass it, encapsulate it instead ;)</p> <p>Make a new UIView subclass - your header file would look like :</p> <pre><code>@interface MySubclass : UIView @end </code></pre> <p>And just have a UIScrollView as a subview - your .m file would look like :</p> <pre><code>@interface MySubclass () &lt;UIScrollViewDelegate&gt; @property (nonatomic, strong, readonly) UIScrollView *scrollView; @end @implementation MySubClass @synthesize scrollView = _scrollView; - (UIScrollView *)scrollView { if (nil == _scrollView) { _scrollView = [UIScrollView alloc] initWithFrame:self.bounds]; _scrollView.delegate = self; [self addSubView:_scrollView]; } return _scrollView; } ... your code here ... @end </code></pre> <p>Inside your subclass, just always use <code>self.scrollView</code> and it will create the scroll view the first time you ask for it.</p> <p>This has the benefit of hiding the scroll view completely from anyone using your <code>MySubClass</code> - if you needed to change how it worked behind the scenes (i.e. change from a scroll view to a web view) it would be very easy to do :)</p> <p>It also means that no-one can alter how you want the scroll view to behave :)</p> <p>PS I've assumed ARC - change <code>strong</code> to <code>retain</code> and add <code>dealloc</code> if necessary :)</p> <hr> <p>EDIT</p> <p>If you want your class to behave <em>exactly</em> as a UIScrollView then you could try this adding this method (taken from <a href="https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/ObjCRuntimeGuide/Articles/ocrtForwarding.html" rel="nofollow">these docs</a> and untested!) :</p> <pre><code>- (void)forwardInvocation:(NSInvocation *)anInvocation { if ([self.scrollView respondsToSelector:anInvocation.selector]) [anInvocation invokeWithTarget:self.scrollView]; else [super forwardInvocation:anInvocation]; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload