How to create a UIScrollView Programmatically?

后端 未结 7 1073
后悔当初
后悔当初 2020-11-30 23:38

Alright, so the key here is I\'m not using IB at all, because the View I\'m working with is created programmatically. The UIView covers the lower half the scre

相关标签:
7条回答
  • 2020-11-30 23:56

    Create a UiScrollView Programitically

    ScrView = [[UIScrollView alloc]initWithFrame:CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y, self.view.frame.size.width, self.view.frame.size.height)];
    ScrView.showsVerticalScrollIndicator=YES;
    [ScrView setBackgroundColor:[UIColor yellowColor]];
    [ScrView setScrollEnabled:YES];
    [ScrView setContentSize:CGSizeMake(0, 700)];
    [ScrView setShowsHorizontalScrollIndicator:YES];
    [ScrView setShowsVerticalScrollIndicator:YES];
    [self.view addSubview:ScrView];
    
    0 讨论(0)
  • 2020-12-01 00:04

    Calculate distance between 2 point on maps for iOS [closed]

    import UIKit import CoreLocation

    class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        var myLocation = CLLocation(latitude: 23.1929, longitude: 72.6156)
        var myBuddysLocation = CLLocation(latitude: 23.0504, longitude: 72.4991)
        var distance = myLocation.distance(from: myBuddysLocation) / 1000
        print(String(format: "The distance to my buddy is %.01fkm", distance))
    }
    

    }

    0 讨论(0)
  • 2020-12-01 00:05

    try this,

    {
    
            scrollview=[[UIScrollView alloc]initWithFrame:CGRectMake(0,0,320,480)]; 
            scrollview.showsVerticalScrollIndicator=YES;
            scrollview.scrollEnabled=YES;
            scrollview.userInteractionEnabled=YES;
            [self.view addSubview:scrollview];
            scrollview.contentSize = CGSizeMake(width,height);
    
        [scrollview release];
    }
    
    0 讨论(0)
  • 2020-12-01 00:15

    Simple Method: You can create multiple times if you need more

    - (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-01 00:16

    This may be helpful to you for creating the uiscrollview programmatically
    http://unconditionalloop.blogspot.com/2011/04/uiscrollview-programmatically.html

      UIScrollView *scrollview = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];   
         NSInteger viewcount= 4;  
         for(int i = 0; i< viewcount; i++) { 
    
            CGFloat y = i * self.view.frame.size.height;  
            UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, y,self.view.frame.size.width, self .view.frame.size.height)];       
            view.backgroundColor = [UIColor greenColor];  
            [scrollview addSubview:view];  
            [view release];  
         }    
      scrollview.contentSize = CGSizeMake(self.view.frame.size.width, self.view.frame.size.height *viewcount);   
      [self.view addSubview:scrollview];
    
    0 讨论(0)
  • 2020-12-01 00:17

    I use lazy a lot when creating UI programmatically, like this:

    class WorldViewController: UIViewController {
        override func loadView() {
            super.loadView()
            view = scrollView
            scrollView.addSubview(label0)
        }
    
        lazy var scrollView: UIScrollView = {
            let instance = UIScrollView()
            instance.backgroundColor = UIColor.blackColor()
            return instance
        }()
    
        lazy var label0: UILabel = {
            let instance = UILabel()
            instance.text = "Ice caps are melting"
            instance.textColor = UIColor.whiteColor()
            instance.sizeToFit()
            return instance
        }()
    
        var needLayoutContent = true
    
        override func viewDidLayoutSubviews() {
            super.viewDidLayoutSubviews()
            if needLayoutContent {
                let bounds = scrollView.bounds
                let contentSize = CGSizeMake(bounds.width * 1.5, bounds.height * 1.5)
                label0.center = CGPointMake(contentSize.width / 2, contentSize.height / 2)
                scrollView.contentSize = contentSize
                scrollView.contentOffset = CGPointMake(bounds.width * 0.25, bounds.height * 0.25)
                needLayoutContent = false
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题