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
Unexpected Non-Void Return Value in Void Function Swift3
Add a Uiview Above All, Even the Navigation Bar
How to Disable/Enable the Sleep Mode Programmatically in iOS
Refresh Devices in Team Provisioning Profile Managed by Xcode 7
iOS 11 - Keyboard Height Is Returning 0 in Keyboard Notification
Is a Date in Same Week, Month, Year of Another Date in Swift
How to Change the Background Color of the Uialertcontroller
Popover in Swift 3 on iPhone iOS
Copy Nsattributedstring in Uipasteboard
Creating Framework That Requires (Depends On) Another Framework
Automatically Adjustable View Height Based on Text Height in Swiftui
How to Use Presentmodalviewcontroller to Create a Transparent View
Ios: Uibutton Resize According to Text Length
Send a File as Attachment in Objective C
Uiapplication.Registerforremotenotifications() Must Be Called from Main Thread Only
Launch Image Not Showing Up in iOS Application (Using Images.Xcassets)
The Supportedinterfaceorientations Method Doesn't Override Any Method from Its Superclass