简体   繁体   中英

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.

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. Then add the [button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside] method after where you create the button.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM