【问题标题】:How to create a UIScrollView Programmatically?如何以编程方式创建 UIScrollView?
【发布时间】:2011-03-01 05:05:21
【问题描述】:

好的,这里的关键是我根本没有使用 IB,因为我正在使用的视图是通过编程方式创建的。 UIView 覆盖了屏幕的下半部分,上面有一堆按钮。但是,我想在UIView 中添加更多按钮,而不使其变得更大。为此,我想在视图中创建一个UIScrollView,这将允许我在屏幕外添加更多按钮,以便用户可以滚动到它们。我认为这就是它的工作原理。

self.manaView = [[[UIView alloc] initWithFrame:frame] autorelease];
self.manaView.backgroundColor = [UIColor purpleColor];

UIScrollView *scroll = [UIScrollView alloc];
scroll.contentSize = CGSizeMake(320, 400);
scroll.showsHorizontalScrollIndicator = YES;
[self.manaView addSubview:scroll];

代码的第一部分启动了我的UIView,效果很好,但我不知道如何以编程方式制作UIScrollView并将其添加到视图中,然后将按钮添加到其中。

UIButton *ret2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
ret2.tag = 102;
ret2.frame = CGRectMake(255, 5, 60, 50);
[ret2 setTitle:@"Return" forState:UIControlStateNormal];
[ret2 addTarget:self action:@selector(flipAction:) forControlEvents:UIControlEventTouchUpInside];
[scroll addSubview:ret2];

当我这样做时,按钮从我的屏幕上消失了。那么我该如何正确地做到这一点呢?感谢您的帮助!

【问题讨论】:

    标签: iphone objective-c uiscrollview


    【解决方案1】:

    代替:

    UIScrollView *scroll = [UIScrollView alloc];
    

    执行此操作(将框架设置为您希望滚动视图的大小):

    UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:...];
    

    【讨论】:

    • 啊,我必须有 initwithframe 和 .contentSize 才能让它工作。谢谢!
    【解决方案2】:

    这可能对您以编程方式创建 uiscrollview 有所帮助
    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];
    

    【讨论】:

    • 我相信你还需要[self.view addSubview:scrollview]; 才能工作。
    【解决方案3】:

    试试这个,

    {
    
            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];
    }
    

    【讨论】:

    • 我使用了 NANNAV 回答的最后一个选项,看起来效果很好,但现在我可以看到一个滚动条,但我的 UI 已锁定。这意味着唯一有效的控件是 UIScrollView...任何想法...
    【解决方案4】:

    以编程方式创建 UiScrollView

    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];
    

    【讨论】:

      【解决方案5】:

      在以编程方式创建 UI 时,我经常使用 lazy,如下所示:

      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
              }
          }
      }
      

      【讨论】:

      • 效果很好@neoneye!
      【解决方案6】:

      简单方法: 如果您需要更多,您可以创建多次

      - (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];
          }
      }
      

      【讨论】:

        【解决方案7】:

        计算 iOS 地图上 2 点之间的距离 [关闭]

        导入 UIKit 导入核心位置

        类视图控制器: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))
        }
        

        }

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2013-06-17
          • 1970-01-01
          • 2015-05-19
          • 2015-05-28
          • 2010-09-15
          • 1970-01-01
          • 2013-03-10
          • 2013-11-15
          相关资源
          最近更新 更多