8月
7
7
UISegmentedControlのタイトルの位置を変更する方法

どうも、@takashingsです。
仕事でUISegmentedControlを使うことがあって、
タイトルを設定しようとしたら、タイトルの位置が微妙に中心に来ていないんですよね。
むむむ…ということで、調べたら、調整する方法を見つけたので、紹介したいと思います。
ちなみに何も設定しないままのUISegmentedControlはこちら。

AppStoreのランキングの文字、そのまま持ってきました。
すごく細かいですが、若干上に寄っているんですよね。
調整するために使用するのは”setContentOffset:forSegmentAtIndex“というメソッド。
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:@"トップ有料",@"トップ無料",@"トップセールス", nil]]; segmentedControl.selectedSegmentIndex = 0; // ここで調整する [segmentedControl setContentOffset:CGSizeMake(0, 2) forSegmentAtIndex:0];
位置はCGSizeで設定します。
これでUISegmentedControl内のタイトル位置を調整することができます。
上で調整したものがこちら。

ちゃんと真ん中にタイトルの位置が来ていますね。

ちなみに、セグメントごとに位置を調整できるようです。
設定次第ではこんなこともできます。

セグメントごとに位置を変えるということはあまりなさそうですが、こんなこともできるよ、ということで。