1
votes

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

2

2 Answers

2
votes

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)];
0
votes

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.