6 개의 버튼을 만들었습니다. 그러나 그들은 화면의 중앙에 있지 않습니다. 이 버튼에 대한 제약을 어떻게 만들 수 있습니까?
var x = 50;
var y = 50;
var n=6
int index = 0;
while (index < n)
{
if (index % 3 == 0)
{
y += 70;
x = 30;
}
UILabel btn = new UILabel(new CGRect(x, y, 90, 70));
btn.BackgroundColor = UIColor.Red;
btn.TextAlignment = UITextAlignment.Center;
btn.Text = "botton" + index;
x += 60;
index++;
this.View.add(btn)
}
addButtons
화면 중앙에 버튼을 추가 하는 간단한 방법을 작성했습니다 .
public override void ViewDidLoad ()
{
base.ViewDidLoad ();
// Perform any additional setup after loading the view, typically from a nib
for (int i = 0; i < 6; i++)
{
addButtons(View,i);
}
}
void addButtons(UIView backView, int buttonNumber) {
//number of buttons in one line
int cols = 3;
//add it to buttonY to change the start position of Y
var startYPadding = 30;
var buttonWidth = 90;
var buttonHeight = 70;
var colMargin = (backView.Bounds.Size.Width - (cols * buttonWidth)) / (cols + 1);
var col = buttonNumber % cols;
var row = buttonNumber / cols;
//if you want the paddings between buttons use below code
//var buttonX = col * (buttonWidth + colMargin)+ colMargin;
//var buttonY = row * (buttonHeight + colMargin) + startYPadding;
//if you dont want the padding between buttons use below code
var buttonX = (backView.Bounds.Size.Width - (cols * buttonWidth))/2 + col*buttonWidth;
var buttonY = row * buttonHeight + startYPadding;
UILabel btn = new UILabel(new CGRect(buttonX, buttonY, buttonWidth, buttonHeight));
btn.BackgroundColor = UIColor.Red;
btn.TextAlignment = UITextAlignment.Center;
btn.Text = "botton" + buttonNumber;
backView.Add(btn);
}
질문이 있으면 언제든지 저에게 물어보십시오.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다