通常のUIButtonをどのように整列させることができますか?プログラムで上、下、または真ん中に?
5 に答える
UIButton
自分の枠を設定する必要があります。
水平方向の配置
float X_Co = (self.view.frame.size.width - yourButtonWidth)/2;
[button setFrame:CGRectMake(X_Co, yourYposition, yourButtonWidth, yourButtonheight)];
垂直方向に整列
float Y_Co = (self.view.frame.size.height - yourButtonheight)/2;
[button setFrame:CGRectMake(yourXposition, Y_Co, yourButtonWidth, yourButtonheight)];
左上
[button setFrame:CGRectMake(0.0, 0.0, yourButtonWidth, yourButtonheight)];
右上
float X_Co = self.view.frame.size.width - yourButtonWidth;
[button setFrame:CGRectMake(X_Co, 0.0, yourButtonWidth, yourButtonheight)];
左下の
float Y_Co = self.view.frame.size.height - yourButtonheight;
[button setFrame:CGRectMake(0.0, Y_Co, yourButtonWidth, yourButtonheight)];
右下
float X_Co = self.view.frame.size.width - yourButtonWidth;
float Y_Co = self.view.frame.size.height - yourButtonheight;
[button setFrame:CGRectMake(X_Co, Y_Co, yourButtonWidth, yourButtonheight)];
セルフ ビューの中心
button.center = self.view.center;
OR
float X_Co = (self.view.frame.size.width - yourButtonWidth)/2;
float Y_Co = (self.view.frame.size.height - yourButtonheight)/2;
[button setFrame:CGRectMake(X_Co, Y_Co, yourButtonWidth, yourButtonheight)];
ボタンの中心を設定して、それに応じて配置できます
yourButton.center = CGPointMake(0.0, 0.0);// for topleft
yourButton.center = CGPointMake(160.0, 240.0);// for center
yourButton.center = CGPointMake(320.0, 480.0);// for bottomright
お役に立てれば
サブビューの位置を設定する唯一の方法は手動です。たとえば、次のようにフレームを設定できます。
または、次のように原点を変更します。例:
view.frame.origin.x = INT_VALUE;
ある種のアラインメントを取得する唯一の方法は、で数学を行うことですself.view.frame
。オブジェクトを画面の中央に水平に配置する場合は、次を使用します。
view.frame.origin.x = self.view.frame.size.width / 2
44ピクセルのマージンで何かを上に揃えたい場合は、次を使用します。
view.frame.origin.y = self.view.frame.origin.y = (view.frame.size.height / 2) + 44;
等。
フレームを設定する必要があります。
何かのようなもの
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(320, 120, 35, 10)];
また
button.frame = CGRectMake(320, 120, 35, 10);
最初の2つの値はそれぞれビューの幅と高さ(XとYの位置)であり、2番目の値はボタン自体の幅と高さであることに注意してください。
したがって、上部にボタンが必要な場合は、次のように入力する必要があります
button.frame = CGRectMake(0, 0, 35, 10);
真ん中
button.frame = CGRectMake(160, 240, 35, 10);
下
button.frame = CGRectMake(320, 480, 35, 10);
@TheTigerの素晴らしいヒント!このロジックを迅速なクラスに入れました:
https://github.com/iurims/iSLibs/blob/master/ISPositionCalculator.swift