【问题标题】:Horizontal UIScrollView in iOSiOS 中的水平 UIScrollView
【发布时间】:2016-05-19 00:17:06
【问题描述】:

如何在 iOS 中创建水平的UIScrollView 使其看起来像图像?

float width = self.view.frame.size.width/3;
    int x = width;
    float height = scrCategory.frame.size.height;
    for(int i = 0;i<arrCategory.count;i++)
    {
        UIButton *btnCategory = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        btnCategory.frame = CGRectMake(x, 0, width, height);
        [btnCategory setTag:i+1];
        [btnCategory setTitle:[NSString stringWithFormat:@"%@",[arrCategory objectAtIndex:i]] forState:UIControlStateNormal];
        [btnCategory setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [btnCategory addTarget:self action:@selector(btnCategoryAction:) forControlEvents:UIControlEventTouchUpInside];
        [btnCategory setBackgroundColor:[UIColor blackColor]];
        [scrCategory addSubview:btnCategory];
        x = x + width;
    }
    [scrCategory setContentSize:CGSizeMake(x+width, height)];

【问题讨论】:

  • 到目前为止你尝试了什么?
  • 你能把你的代码贴出来吗?
  • 在ScrollView中创建多个标签,并设置它们的宽度。
  • @khuong291 我也做到了..
  • 你设置了scrollview的宽度吗?

标签: ios objective-c cocoa-touch uiscrollview


【解决方案1】:

使用 iCarousel https://github.com/nicklockwood/iCarousel 要么 将 UICollectionview 与分页一起使用。

设置固定的背景图片(带选择的黑条)。

【讨论】:

    【解决方案2】:
        int x =10;
        int y = 5;
        int height = 90;
        self.scrlView.contentInset = UIEdgeInsetsZero;
    
        if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
        {
            for(int i=0;i<images.count;i++)
            {
                UIButton *intrestBtn=[UIButton buttonWithType:UIButtonTypeRoundedRect];
                [intrestBtn setFrame:CGRectMake(x, y, 80, 80)];
                [self.scrlView addSubview:intrestBtn];
    
                x = x +95;
                self.scrlView.contentSize = CGSizeMake(x+SCREEN_WIDTH,90 );
                [self.scrlView setShowsHorizontalScrollIndicator:NO];
            }
    

    这是我以前的做法

    【讨论】:

      【解决方案3】:

      为什么不直接使用 UICollectionView? 您只需要创建带有 1 个标签的自定义单元格。无需自定义布局,只需配置UICollectionViewFlowLayout

      【讨论】:

        猜你喜欢
        • 2012-11-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-11-10
        • 1970-01-01
        • 1970-01-01
        • 2011-05-18
        • 1970-01-01
        相关资源
        最近更新 更多