简体   繁体   English

如何移动按钮动作iOS

[英]How to move button action ios

i want to change positon of button when it is pressed, it should move below again and agin when keep pressing , please tell me what should i do? 我想在按下按钮时更改按钮的位置,它应该再次移至下方并再次按下时再次显示,请告诉我该怎么办?

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;

After creating UIButton and setting it's frame like UITextField, you should use button's setFrame: method to change the position of it. 创建UIButton并将其框架设置为UITextField之后,应使用按钮的setFrame:方法更改其位置。

If you like to change the position with pressing button, first create a -(IBAction *)buttonPressed:(id)sender method and set button's new position in it. 如果您想通过按下按钮更改位置,请首先创建-(IBAction *)buttonPressed:(id)sender方法,然后在其中设置按钮的新位置。 Then add the [button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside] method after where you create the button. 然后在创建按钮之后添加[button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside]方法。

Give the frame of the button in you described snippet only. 仅在您描述的代码段中提供按钮的框架。

For ex, 例如,

<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..

Enjoy Programming! 享受编程!

you can assign an tag to the button as 您可以为按钮分配标签为

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

and whenever the button is clicked the frame for the button can be changed by getting a reference of the button.In the code below i have moved down the button by 20 pixels. 每当单击按钮时,都可以通过获取按钮的引用来更改按钮的框架。在下面的代码中,我将按钮向下移动了20个像素。 You can change this value to what ever you want . 您可以将此值更改为所需的值。

 -(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)];
}

You have to modify your code by: 您必须通过以下方式修改代码:

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