2016-10-05 63 views
1

如何在頂部創建水平滾動視圖,如具有多個按鈕的滾動菜單(例如:20個按鈕)? 請幫忙!具有n個按鈕的水平滾動視圖:[iOS]

我使用下面的代碼:

CGRect buttonFrame = CGRectMake(0.0f, 0.0f, scrollView.frame.size.width, scrollView.frame.size.height); 
    for (NSString *text in wordsArray) { 
     UIButton *button = [[UIButton alloc]initWithFrame:buttonFrame]; 
     button.text = text; 
     [scrollView addSubview:button]; 

     buttonFrame.origin.x +=buttonFrame.size.width; 
    } 

    CGSize contentSize = scrollView.frame.size; 
    contentSize.width = buttonFrame.origin.x; 
    scrollView.contentSize = contentSize; 

,但沒有得到我想要的東西。

+0

請檢查這個答案,我認爲這是一個,你在找什麼http://stackoverflow.com/questions/6688160/how-to-programmatically-add-a -uisegmentedcontrol-to-a-container-view/6689592#6689592 – Wolverine

回答

3

我試圖解決這個問題,可能是解決你的問題

NSMutableArray *wordsArray = [NSMutableArray arrayWithObjects:@"AAA", @"BBB", @"CCCC", @"dd", @"eeeee", @"ffff", @"g", @"hhh", @"iiiiiii", @"jjjj", @"kkkkk", @"lllll", nil]; 

CGFloat btnFrameX = 10.0; 
CGFloat Width = self.scrollview.frame.size.width; 
CGFloat Height = self.scrollview.frame.size.height; 
UIButton *button; 
for (NSString *text in wordsArray) { 

    button = [UIButton buttonWithType:UIButtonTypeCustom]; 
    [button setBackgroundColor:[UIColor orangeColor]]; 
    [button.titleLabel setFont:[UIFont systemFontOfSize:20.0f]]; 
    [button setTitle:text forState:UIControlStateNormal]; 

    button.frame = CGRectMake(btnFrameX, 20, Width, Height); 

    [self.scrollview addSubview:button]; 

    btnFrameX = btnFrameX + Width + 5; 
} 

CGSize contentSize = self.scrollview.frame.size; 
contentSize.width = wordsArray.count * (Width + btnFrameX); 
self.scrollview.contentSize = contentSize; 

截圖:

enter image description here

快樂編碼...

1

試試這個:

CGFloat buttonX = 0.f; 
CGFloat buttonY = 0.f; 
CGFloat buttonWidth = scrollView.frame.size.width; 
CGFloat buttonHeight = scrollView.frame.size.height; 
for (int i = 0; i < wordsArray.count; i++) 
{ 
    UIButton *button = [UIButton new]; 
    [button setTitle:wordsArray[i] forState:UIControlStateNormal]; 
    [scrollView addSubview:button]; 

    buttonX = i * buttonWidth; 
    button.frame = CGRectMake(buttonX, buttonY, buttonWidth, buttonHeight); 
} 

CGSize contentSize = scrollView.frame.size; 
contentSize.width = wordsArray.count * buttonWidth; 
scrollView.contentSize = contentSize; 
+0

button.text給出錯誤: - 在UIButton類型的對象上找不到文本 –

+0

嗯,它的一個按鈕是固定的。 – CHwang

+0

按鈕之間的距離很大,如何減少按鈕之間的距離 –

1

可以使用的CollectionView使用自定義collectionViewCell並設置其屬性如下:

[collectionView setScrollDirection:UICollectionViewScrollDirectionHorizontal]; 

[collectionView setPagingEnabled:NO]; 
1

不喜歡

CGFloat btnFrameX = 80.0; // customize the x and y as per your need 
CGFloat Width = scrollView.frame.size.width; 
CGFloat Height = scrollView.frame.size.height; 

for (int i = 1 ; i <= wordsArray.count; i++) 
{ 
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
[button addTarget:self action:@selector(callButtonMethod:) forControlEvents:UIControlEventTouchUpInside]; 
button.tag = i; 
[button setTitle:wordsArray[i] forState:UIControlStateNormal]; 
button.frame = CGRectMake(btnFrameX, 0, Width, Height); 
[scrollview addSubView:button]; 
btnFrameX = btnFrameX + Width + 5; 
} 
scrollview.contentSize = CGSizeMake(btnFrameX + 50, yourHeight); 

調用方法,像

-(void) callButtonMethod:(UIButton *) sender 
{ 
    //you can access your button by using tag. 
} 

或者使用UICollectionView爲你的概念

1

不幸的是我的代碼是在Swift中,b你會得到它的要點。設置按鈕的標題後,可以調用button.sizeToFit()。這樣它將使用首選的寬度。確定下一個按鈕的原點時,請注意使用按鈕的新寬度。這就是我的Swift類。

import Foundation 
import UIKit 

class ButtonsView: UIView { 

    private var scrollView: UIScrollView? 

    @IBInspectable 
    var wordsArray: [String] = [String]() { 
     didSet { 
      createButtons() 
     } 
    } 

    private func createButtons() { 
     scrollView?.removeFromSuperview() 
     scrollView = UIScrollView(frame: CGRect(x: 0, y: 0, width: self.frame.size.width, height: self.frame.size.height)) 
     self.addSubview(scrollView!) 
     scrollView!.backgroundColor = UIColor.gray 

     let padding: CGFloat = 10 
     var currentWidth: CGFloat = padding 
     for text in wordsArray { 
      let button = UIButton(frame: CGRect(x:currentWidth, y: 0.0, width: 100, height: self.frame.size.height)) 
      button.setTitle(text, for: .normal) 
      button.sizeToFit() 
      let buttonWidth = button.frame.size.width 
      currentWidth = currentWidth + buttonWidth + padding 
      scrollView!.addSubview(button) 
     } 
     scrollView!.contentSize = CGSize(width:currentWidth,height:scrollView!.frame.size.height) 
    } 
} 

Example