2

I am using a view in main ViewController, and want to move it in a new position with a button click. I have already tried to use different method...

.h file

@property (nonatomic, strong) IBOutlet UIView *UIview3;

.m file

@synthesize UIview3;

and I have implemented the following methods.

Method: 1

CGRect f = self.UIview3.frame;
f.origin.x = 28; // new x
f.origin.y = 74; // new y
self.UIview3.frame = f;

Method: 2

UIView *newview = [[UIView alloc]initWithFrame:
CGRectMake(28, 74, 261, 119)];
[self.UIview3 addSubview:newview];

other methods (Non of this work)

UIview3.frame = CGRectMake(28, 231, 261, 119);
UIview3.center = CGPointMake( 28,74);
[_UIview3 setFrame:CGRectMake(28, 74, 261, 119)];
[_UIview3 setFrame:CGPointMake( 28,74)];
[self.UIview3 setCenter:CGPointMake(28, 74)];

Anyone to help ? Thanks in Advance. sohanoor

soumya
  • 3,801
  • 9
  • 35
  • 69
sohanoor
  • 129
  • 2
  • 12

4 Answers4

2

You can try something like this:

[UIView animateWithDuration:0.1
        delay:0.1
        options: nil
        animations:^
                  {
                      [UIview3 setFrame:CGRectMake(28, 74, 261, 119)];
                  }
        completion:^(BOOL finished)
                  {

                  }
];

You can set the frame like this:

[UIview3 setFrame:CGRectMake(28, 74, 261, 119)];

Don't use new keyword to reallocate UIview3 since it is wired through IB.

NeverHopeless
  • 11,077
  • 4
  • 35
  • 56
0

try these on your UIButtons click event..

[UIView animateWithDuration:0.5 //animation duration..
                      delay:0.2   //animation delay..
                    options: nil
                 animations:^{
                  //Write the coordinates you wants..
                     [_UIview3 setFrame:CGRectMake(28, 74, 261, 119)];
                 }
                 completion:^(BOOL finished){
                     NSLog(@"Done!");

                 }];

i hope it helps..

krushnsinh
  • 874
  • 1
  • 10
  • 11
0

You used the following method, but didn't changed the centre of the view you intend to move. You set it as it was earlier.

[self.UIview3 setCenter:CGPointMake(28, 74)];

Please change its centre points to move to a new position in the IBAction of the button as following and see the change.

[self.UIview3 setCenter:CGPointMake(100, 100)];

For animation purpose, see the above animation block posted by NeverHopeless

Shubham Ojha
  • 461
  • 5
  • 17
0

First you have to check autolayout is enable or not from storyboard

enter image description here

If autolayout not enable than you can set frame and move your view bt If autolayout is enable than you have to add proper constraint in storyboard or use this for manual contraint set

UIView *newview = [[UIView alloc]initWithFrame:
                   CGRectMake(28, 74, 261, 119)];
[UIview3 addSubview:newview];

newview.translatesAutoresizingMaskIntoConstraints = NO;

NSLayoutConstraint *topX = [NSLayoutConstraint constraintWithItem:newview
                                                        attribute:NSLayoutAttributeTop
                                                        relatedBy:NSLayoutRelationEqual
                                                           toItem:UIview3
                                                        attribute:NSLayoutAttributeTop
                                                       multiplier:1
                                                         constant:28];

NSLayoutConstraint *leftY = [NSLayoutConstraint constraintWithItem:newview
                                                         attribute:NSLayoutAttributeLeft
                                                         relatedBy:NSLayoutRelationEqual
                                                            toItem:UIview3
                                                         attribute:NSLayoutAttributeLeft
                                                        multiplier:1.0
                                                          constant:74];
[newview addConstraints:@[
                          [NSLayoutConstraint constraintWithItem:newview attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutRelationEqual multiplier:1 constant:261],
                          [NSLayoutConstraint constraintWithItem:newview attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutRelationEqual multiplier:1 constant:119]]];

[UIview3 addConstraints:@[topX,
                          leftY
                          ]];

for moving newview you can use this code

topX.constant = 50;
leftY.constant = 100;

[newview updateConstraintsIfNeeded];

[UIView animateWithDuration:0.2 animations:^{
    [newview layoutIfNeeded];
}];

if you set contraint from storyboard than you can simply create IBOutlet of your x nd y constraint than use above code for moving view

Pooja Patel
  • 626
  • 8
  • 12