1

According to my project, I changed the default view that tied to one UIViewController from UIView to UIScrollView as it has to be changed its offset and content size. The way I change from UIView to UIScrollView was by Interface Builder > Custom Class

If I want to change its offset and content size programmatically within The Controller, It can't be done in usual way unless I cast it

[((UIScrollView *)self.view) setContentSize:CGSizeMake(1024, 2000)];
[((UIScrollView *)self.view) setContentOffset:CGPointMake(0, 150)];

But with other UIScrollView within the app, changing their offset and content size can be done like any other,

// Assume that sv is scroll view object
[sv setContentSize:CGSizeMake(1027, 2000)];
[sv setContentOffset:CGPointMake(0, 150)];

When I print that scroll view object, it shown as a UIScrollView

NSLog(@"%@", self.view);
NSLog(@"%@", [self.view class]);

-> <UIScrollView: 0x6d3d3f0; frame = (0 0; 1024 768); autoresize = RM+BM; layer = <CALayer: 0x6d52ce0>; contentOffset: {0, 0}>
-> UIScrollView

Anyone know what I did missing or forgot to do rather than change the view's custom class?

Best and Thank you, Tar

P.S. Besides the extra code needed, everything works fine. But, I can't take the chance of possible bug in the future. This client is super crazy, no error will be accepted

Tar_Tw45
  • 3,122
  • 6
  • 35
  • 58

2 Answers2

2

An alternative to this (that would avoid the need for casting) would be to set the UIViewController custom class back to UIView in interface builder. Then, insert a new UIScrollView as a subview (simply drag and drop in Interface Builder), connecting the UIScrollView to the UIViewController subclass via an IBOutlet that looks something like:

@property (nonatomic, weak) IBOutlet UIScrollView *scrollView;

(When you connect the outlet that property declaration should be generated for you by XCode).

Then, in your UIViewController subclass, you can do something like:

[[self scrollView] setContentSize:CGSizeMake(1024, 2000)];
[[self scrollView] setContentOffset:CGPointMake(0, 150)];
Will Pragnell
  • 3,073
  • 1
  • 20
  • 21
  • Thanks Will, I'm appreciate your help. But, doing such thing might break the application as thousand lines of code were made within this controller and several controller and custom class were wired with it. I can simply do search & replace but prefer not to touch any code for now. If there is not a better solution, I'll do this. Anyway, do you know what cause the problem, why do I have to cast it? Thanks again – Tar_Tw45 Apr 20 '12 at 11:03
  • 1
    Ok. I think the problem is because the `view` property on `UIViewController` is declared as a `UIView`. Have you tried not using dot notation at all (e.g. `[[self view] setContentSize:CGSizeMake(123, 123)];)` ? – Will Pragnell Apr 20 '12 at 11:17
  • 1
    On second thoughts, I don't think that not using dot notation should make any difference. I think casting is your only sensible option if you want the `UIViewController`'s top level view to be a `UIScrollView`. – Will Pragnell Apr 20 '12 at 11:23
  • Hey, thanks again will. Sorry for late reply, I was out for holiday. I tried not using dot notation, the result was same. Anyways, appreciate your help : ) – Tar_Tw45 Apr 23 '12 at 02:49
  • Got another question, I create an IBOutlet of UIScrollView, named it as mainScrollView and then tied the UIScrollView that I changed its custom class by Interface Builder to it. In this case, I'll be able to do "[self.mainScrollView setContentSize:CGSizeMake(x, y)]" .... Will there be any problem? (The view now tied to UIViewController's view and the IBOutlet. – Tar_Tw45 Apr 23 '12 at 03:00
  • OK, I got an answer from another thread, http://stackoverflow.com/questions/646521/how-do-i-override-the-view-property-in-uiviewcontroller According to an answer in that thread, casting should be better. – Tar_Tw45 Apr 23 '12 at 03:25
  • 1
    Yeah, I'm quite sure I understand exactly what you did there, but casting sounds simpler (and therefore better) to me. – Will Pragnell Apr 23 '12 at 13:51
0

I'm not sure what the best solution is but a quick workaround is to create a method that casts the view in the UIViewController. That is:

- (UIScrollView *)scrollView {
   return (UIScrollView *)self.view;
}

Then, instead of referring to the view in the ViewController as self.view, refer to it as self.scrollView.

garretriddle
  • 409
  • 3
  • 10