Implementing UIScrollView programmatically

前端 未结 3 1687
执笔经年
执笔经年 2020-12-18 10:02

In the page control sample from apple there is a ScrollView in the interface builder. It is linked with the corresponding IBOutlet. I want to change the code so this is all

相关标签:
3条回答
  • 2020-12-18 10:08

    you simply put this inside awakeFromNib method

    scrollView=[[UIScrollView alloc] initWithFrame:CGRectMake(0, 20, 320, 440)];
    

    no need to add as subview because in phoneContentController xib there are no views inside that.so how can you add like[ self.view addSubView:ScrollView];this is because phoneContentController is not type of UIViewController Class. it's a sub Class of ContentController which is subClass of NSObject inside that project.

    0 讨论(0)
  • 2020-12-18 10:15

    Simple Method: You can created multiple times if you need means

    - (void)viewDidLoad
    {
        [super viewDidLoad];
    
        int x = 0;
        int y = 10;
        for(int i=0; i<5; i++)
        {
            UIScrollView *scrollview=[[UIScrollView alloc]initWithFrame:CGRectMake(x, y, 50, 50)];
            scrollview.showsVerticalScrollIndicator=YES;
            scrollview.scrollEnabled=YES;
            scrollview.userInteractionEnabled=YES;
            scrollview.backgroundColor = [UIColor greenColor];
            [self.view addSubview:scrollview];
            //scrollview.contentSize = CGSizeMake(50,50);
            x=x+55;
    
            //[self myscrollView];
        }
    }
    
    0 讨论(0)
  • 2020-12-18 10:23

    Since you're not loading the interface from a nib file, you should set up your UIScrollView in your PhoneContentController's init method:

    - (id)init
    {
        [super init];
    
        if (self) {
            scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 20, 320, 440)];
            pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(<#CGFloat x#>, <#CGFloat y#>, <#CGFloat width#>, <#CGFloat height#>)]; // Place it where you want it.
            viewControllers = [[NSMutableArray alloc] init];
    
            // load our data from a plist file inside our app bundle
            NSString *path = [[NSBundle mainBundle] pathForResource:@"content_iPhone" ofType:@"plist"];
            self.contentList = [NSArray arrayWithContentsOfFile:path];
    
            // view controllers are created lazily
            // in the meantime, load the array with placeholders which will be replaced on demand
            NSMutableArray *controllers = [[NSMutableArray alloc] init];
            for (unsigned i = 0; i < kNumberOfPages; i++)
            {
                [controllers addObject:[NSNull null]];
            }
            self.viewControllers = controllers;
            [controllers release];
    
            // a page is the width of the scroll view
            scrollView.pagingEnabled = YES;
            scrollView.contentSize = CGSizeMake(scrollView.frame.size.width * kNumberOfPages, scrollView.frame.size.height);
            scrollView.showsHorizontalScrollIndicator = NO;
            scrollView.showsVerticalScrollIndicator = NO;
            scrollView.scrollsToTop = NO;
            scrollView.delegate = self;
    
            pageControl.numberOfPages = kNumberOfPages;
            pageControl.currentPage = 0;
    
            // pages are created on demand
            // load the visible page
            // load the page on either side to avoid flashes when the user starts scrolling
            //
            [self loadScrollViewWithPage:0];
            [self loadScrollViewWithPage:1];
        }
    
        return self;
    }
    

    In your AppDelegate, make the following changes:

    - (void)applicationDidFinishLaunching:(UIApplication *)application
    {
        self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    
        if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
            contentController = [[PhoneContentController alloc] init];
        } else {
            contentController = [[PadContentController alloc] init];
        }
    
        [self.window addSubview:contentController.view];
        [window makeKeyAndVisible];
    }
    
    0 讨论(0)
提交回复
热议问题