Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert UIViewController to UIScrollViewController

I am new to iPad developer,

I made one Registration form in my application, when i see my application in Portrait mode, i am able to see whole form with no scrolling, but when i see same form in Landscape mode, i am not able to see part which is at bottom of page, for that a scrolling should be there to see bottom part.

s: In my .h file when i replace

@interface ReminderPage : UIViewController{
...
...
}

:UIViewController with :UIScrollView

and then when i add label in my .m file like this,

UILabel *Lastpaidlbl = [[[UILabel alloc] initWithFrame:CGRectMake(70 ,400, 130, 50)]autorelease];
    Lastpaidlbl.backgroundColor = [UIColor greenColor];
    Lastpaidlbl.font=[UIFont systemFontOfSize:20];
    Lastpaidlbl.text = @"Lastpaid on :";
    [self.view addSubview:Lastpaidlbl];

I am getting error on last line Property view not found on object of type classname. i am unable to add label in my view.

Any help will be appreciated.

like image 502
Krunal Avatar asked Jul 06 '12 07:07

Krunal


2 Answers

The question appears to be really asking how can all the components on the screen be placed inside a UIScrollView, rather than a UIView. Using Xcode 4.6.3, I found I could achieve this by simply:

  • In Interface Builder, select all the sub-views inside the main UIView.
  • Choose Xcode menu item "Editor | Embed In | Scroll View".

The end result was a new scroll view embedded in the existing main UIView, will all the former sub-views of the UIView now as sub-views of the UIScrollView, with the same positioning.

like image 75
Scott Avatar answered Nov 15 '22 01:11

Scott


If you want to replace your UIViewController with a UIScrollView, you will have to go a bit of refactoring to your code. The error you get is just an example of that:

the syntax:

[self.view addSubview:Lastpaidlbl];

is correct if self is a UIViewController; since you changed it to be UIScrollView, you should now do:

[self addSubview:Lastpaidlbl];

You will have quite a few changes like this one to make to your code and will face some issues.

Another approach would be this:

  1. instantiate a UIScrollView (not derive from it);

  2. add your UIView (such as you have defined it) to the scroll view;

  3. define the contentSize of the scroll view so to include the whole UIView you have.

The scroll view acts as a container for your existing view (you add your controls to the scroll view, then add the scroll view to self.view); this way, you could integrate it within your existing controller:

      1. UIScrollView* scrollView = <alloc/init>

      2. [self.view addSubview:scrollView]; (in your  controller)

      3. [scrollView addSubview:<label>]; (for all of your labels and fields).

      4. scrollView.contentSize = xxx;

I think the latter approach will be much easier.

like image 36
sergio Avatar answered Nov 15 '22 00:11

sergio