Simple Way to Change the Position of Uiview

Simple way to change the position of UIView?


aView.center = CGPointMake(150, 150); // set center

or

aView.frame = CGRectMake( 100, 200, aView.frame.size.width, aView.frame.size.height ); // set new position exactly

or

aView.frame = CGRectOffset( aView.frame, 10, 10 ); // offset by an amount

Edit:

I didn't compile this yet, but it should work:

#define CGRectSetPos( r, x, y ) CGRectMake( x, y, r.size.width, r.size.height )

aView.frame = CGRectSetPos( aView.frame, 100, 200 );

UIView changing its position in swift

You have to implement an animation changing the DynView position on click. Here's an example:

@IBAction func btnUp(sender: AnyObject) {
let xPosition = DynView.frame.origin.x
let yPosition = DynView.frame.origin.y - 20 // Slide Up - 20px

let width = DynView.frame.size.width
let height = DynView.frame.size.height

UIView.animateWithDuration(1.0, animations: {
dynView.frame = CGRect(x: xPosition, y: yPosition, width: width, height: height)
})
}

How to position a UIView?

Copying, Modifying and setting the frame again like you have done here is how this is generally done. This can also be done by creating a rect and setting it directly:

UIView.frame = CGRectMake(50,50,50,50);//x,y,w,h

Doing this in an animation block will animate these changes.

Alternitively you can set a views Center point with :

UIView.center = CGPointMake(50,50);

How to Programatically Change Position of UIViews When Autolayout is On

Actually you can layout your _loginTextLabel in - (void)viewDidLayoutSubviews (or - (void)layoutSubviews if it is in your custom view) without autolayout, then you can animate your view like you did before without any bother.

If you layout your views using autolayout from a Nib, then you can create a outlets of your constraint as Abhishek says,basically like this:

[self.view layoutIfNeeded];
yourConstraint.constant = whateverYouWant;
[UIView animateWithDuration:1.0 animations:^{
[containerView layoutIfNeeded]; }
];

Or if you create your constraints programmatically,as apple suggested:https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/AutolayoutPG/AutolayoutPG.pdf

it's basically like you did from a Nib, difference is that you just make your constraints changes in the animation block

[containerView layoutIfNeeded]; 
[UIView animateWithDuration:1.0 animations:^{
// Make all constraint changes here
[containerView layoutIfNeeded]; // Forces the layout of the subtree animation
block and then captures all of the frame changes
}];


Related Topics



Leave a reply



Submit