簡體   English   中英

如何移動按鈕動作iOS

[英]How to move button action ios

我想在按下按鈕時更改按鈕的位置,它應該再次移至下方並再次按下時再次顯示,請告訴我該怎么辦?

textField = [[UITextField alloc] initWithFrame:CGRectMake(76, ([txtFieldArray count] * 30), 191, 25)];
[textField setBorderStyle:UITextBorderStyleLine];
textField.font = [UIFont systemFontOfSize:20];
textField.placeholder = @"Enter text";
textField.autocorrectionType = UITextAutocorrectionTypeNo;
textField.keyboardType = UIKeyboardTypeDefault;
textField.returnKeyType = UIReturnKeyDone;
textField.clearButtonMode = UITextFieldViewModeWhileEditing;
textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
textField.delegate = self;
[myScrollview addSubview:textField];
[txtFieldArray addObject:textField];

CGRect frame = bottomView.frame;
frame.origin.y += textField.frame.size.height + 5;

bottomView.frame = frame;
textField.hidden = NO;

創建UIButton並將其框架設置為UITextField之后,應使用按鈕的setFrame:方法更改其位置。

如果您想通過按下按鈕更改位置,請首先創建-(IBAction *)buttonPressed:(id)sender方法,然后在其中設置按鈕的新位置。 然后在創建按鈕之后添加[button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside]方法。

僅在您描述的代碼段中提供按鈕的框架。

例如,

<button_object>.frame =
CGRectMake(textField.frame.origin.x,textField.frame.origin.y +
x,textField.size.width,textField.size.height);


Where X = the difference upto which you want to add the button after
textfield. For ex, 44 or 50 or so on..

享受編程!

您可以為按鈕分配標簽為

UIButton *btn=[UIButton buttonWithType:UIButtonTypeCustom];
[btn setTag:100];
[btn addTarget:self action:@selector(moveButtonDown) forControlEvents:UIControlEventTouchUpInside];

每當單擊按鈕時,都可以通過獲取按鈕的引用來更改按鈕的框架。在下面的代碼中,我將按鈕向下移動了20個像素。 您可以將此值更改為所需的值。

 -(void)moveButtonDown
{
    UIButton *btn=(UIButton *)[self.view viewWithTag:100];
    float y=btn.frame.origin.y;
    y+=20;
    [btn setFrame:CGRectMake(btn.frame.origin.x, y, btn.frame.size.width, btn.frame.size.height)];
}

您必須通過以下方式修改代碼:

CGRect frame = bottomView.frame;
frame.origin.y += (tField.frame.origin.y+textField.frame.size.height + 5);

bottomView.frame = frame;
textField.hidden = NO;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM