UISegmentedControlで位置、サイズを設定する方法
UISegmentedControlクラスを使おうとしました。

↑こういう選択できるボタンみたいなやつですね。
いざやろうとして困ったのがこのオブジェクトの位置を決める方法。
もちろん、Interface Builderなしです。
UISegmentedControlクラスを使う場合、基本的な流れはこんな感じになるかと。
しかしこれだと、このオブジェクトは行の左端に表示され、位置をコントロールできません。
いろいろと方法はあるのでしょうけど、とりあえず初期化時にこんなので実装しました。
これで、サイズ、位置を設定することができました。

↑こういう選択できるボタンみたいなやつですね。
いざやろうとして困ったのがこのオブジェクトの位置を決める方法。
もちろん、Interface Builderなしです。
UISegmentedControlクラスを使う場合、基本的な流れはこんな感じになるかと。
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:nil]; [segmentedControl insertSegmentWithTitle: @"Male" atIndex: 0 animated: NO ]; [segmentedControl insertSegmentWithTitle: @"Female" atIndex: 1 animated: NO ]; [segmentedControl addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged]; [cell addSubview:segmentedControl]; [segmentedControl release];cellってのからもわかるかもしれませんが、これはtable viewの行に表示する場合。
しかしこれだと、このオブジェクトは行の左端に表示され、位置をコントロールできません。
いろいろと方法はあるのでしょうけど、とりあえず初期化時にこんなので実装しました。
//UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:nil]; UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithFrame:CGRectMake(130, 8, 160, 30)];UISegmentedControlクラスのリファレンスを見ると、初期化はinitWithItems:が記載されてますが、UISegmentedControlの親の親であるUIViewクラスで定義されてるinitWithFrame:で初期化しています。
これで、サイズ、位置を設定することができました。
スポンサーサイト
<< UIViewでのアニメーションが警告ばかり?! TopPage applicationWillTerminate: が呼ばれない理由 >>
トラックバック
トラックバックURL
https://ringsbell.blog.fc2.com/tb.php/401-9e5a49d0
https://ringsbell.blog.fc2.com/tb.php/401-9e5a49d0