0
配列要素を使って動的なボタンを作成する必要があります。 また、ボタンはArrayとscreenboudから複数のUIButtonを追加する
2 button
3 button
4 button
5 button
私のコードのように増やすために、行が賢明です:
- (void)viewDidLoad {
btn_array=[[NSMutableArray alloc]init];
[btn_array addObject:@"Deepak Singh"];
[btn_array addObject:@"Vikas Singh"];
[btn_array addObject:@"Vipin Rawat"];
[btn_array addObject:@"Deepak"];
[btn_array addObject:@"Singh"];
[btn_array addObject:@"Rawat"];
[btn_array addObject:@"Vinita rawat"];
[btn_array addObject:@"Vinod"];
[btn_array addObject:@"OOP"];
[self drawButtonsFromArray:btn_array];
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
-(void)drawButtonsFromArray:(NSArray*)arr
{
UIScreen *mainScreen = [UIScreen mainScreen];
int x=20 ,y=100;
for (int i=0; i<[arr count]; i++)
{
UIButton *btn=[[UIButton alloc]init];
CGSize stringsize = [[arr objectAtIndex:i] sizeWithFont:[UIFont systemFontOfSize:14]];
[btn setTitle:[arr objectAtIndex:i] forState:UIControlStateNormal];
[btn setTitle:[arr objectAtIndex:i] forState:UIControlStateSelected];
[btn setTitle:[arr objectAtIndex:i] forState:UIControlStateHighlighted];
CALayer *btnLayer = [btn layer];
[btnLayer setMasksToBounds:YES];
[btnLayer setCornerRadius:10.0f];
[btn setBackgroundColor:[UIColor redColor]];
btn.titleLabel.adjustsFontSizeToFitWidth = YES;
[btn setFrame:CGRectMake(x,y,stringsize.width, 25)];
int ww=x+btn.frame.size.width;
NSLog(@"X=====%d ",x);
NSLog(@"ww=====%d ",ww);
if (ww>=mainScreen.bounds.size.width) {
x=20;
y=y+35;
//[btn setFrame:CGRectMake(x,y,stringsize.width+15, 25)];
}
else
{
x=x+btn.frame.size.width+5;
}
[self.view addSubview:btn];
}
}
私はあなたの質問が何であるかはっきりしていません。言い換えれば、最初の行にはボタンがあり、次に2行目には3つのボタンがあり、3つ目のボタンには4つのボタンがあります(ボタンのタイトルとして名前テキストを表示するボタンの表示が必要ですか? –
はいいいえ同じ必要があります – iphonemaclover
その場合、UIButtonを作成するのではなく、UICollectionViewを使用して各UICollectionViewCellに名前を印刷し、didSelectItemAtIndexPath:メソッドを使用してそれらが押されたことを検出する方が良いでしょうか? 'sizeWithFont'を使うメソッドも非難されていますが、これはboundingRectWithSizeメソッドに変更する必要があります。 UICollectionViewを使用すると、好きな行にセルを配置することができます。 –