2016-10-05 12 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

ためUICollectionViewを使用し、私はこの問題を解決しようとした、

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

カスタムcollectionViewCellを使用してcollectionViewを使用すると以下のようにそのプロパティを設定することができます。

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

または他のあなたの概念

1

残念ながら私のコードはSwiftにあります。bあなたはそれの要点を得るでしょう。ボタンのタイトルを設定した後、button.sizeToFit()を呼び出すことができます。そうすれば、望ましい幅が使用されます。次のボタンの原点を決定するときは、新しい幅のボタンを使用するように注意してください。これで私のスウィフトクラス。

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

関連する問題