How to get the centre of the view
Swift 4 extension:
extension CGRect {
var center: CGPoint { .init(x: midX, y: midY) }
}
let someRect = CGRect(x: 10, y: 10, width: 200, height: 40)
let theCenter = someRect.center // {x 110 y 30}
How can i get center x,y of my view in android?
centre of the imageView will be
centreX=imageView.getX() + imageView.getWidth() / 2;
centreY=imageView.getY() + imageView.getHeight() / 2;
but make sure you call it after the imageView created
Get center of any UIView in Swift
Original Answer:
I had reproduce your problem easily (see my comment).
I think it could be a problem of auto layout. So may be you can use constraints instead calculate position ?
func addActivityIndicatorToView(activityIndicator: UIActivityIndicatorView, view: UIView){
self.view.addSubview(activityIndicator)
//Don't forget this line
activityIndicator.setTranslatesAutoresizingMaskIntoConstraints(false)
view.addConstraint(NSLayoutConstraint(item: activityIndicator, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
view.addConstraint(NSLayoutConstraint(item: activityIndicator, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))
activityIndicator.startAnimating()
}
Updated Code for Swift 5.0:
- From edit by Rob
NSLayoutAttribute.CenterX
is nowNSLayoutConstraint.Attribute.centerX
NSLayoutRelation.Equal
is nowNSLayoutConstraint.Relation.equal
NSLayoutAttribute.CenterY
is nowNSLayoutConstraint.Attribute.centerY
func addActivityIndicatorToView(activityIndicator: UIActivityIndicatorView, view: UIView){
self.view.addSubview(activityIndicator)
//Don't forget this line
activityIndicatorView.translatesAutoresizingMaskIntoConstraints = false
view.addConstraint(NSLayoutConstraint(item: activityIndicator, attribute: NSLayoutConstraint.Attribute.centerX, relatedBy: NSLayoutConstraint.Relation.equal, toItem: view, attribute: NSLayoutConstraint.Attribute.centerX, multiplier: 1, constant: 0))
view.addConstraint(NSLayoutConstraint(item: activityIndicator, attribute: NSLayoutConstraint.Attribute.centerY, relatedBy: NSLayoutConstraint.Relation.equal, toItem: view, attribute: NSLayoutConstraint.Attribute.centerY, multiplier: 1, constant: 0))
activityIndicator.startAnimating()
}
How to center a subview of UIView
Objective-C
yourSubView.center = CGPointMake(yourView.frame.size.width / 2,
yourView.frame.size.height / 2);
Swift
yourSubView.center = CGPoint(x: yourView.frame.size.width / 2,
y: yourView.frame.size.height / 2)
centering a CGRect in a view
UIView's center
is a property, not a method.
You need to calculate the actual location you want to put it in. You can do this by setting the frame of the view, or by setting its center, which is simpler in your case.
You don't say what view you then go make imageView
a subview of. If you're putting it inside something called superview
, you could do this:
CGPoint superCenter = CGPointMake(CGRectGetMidX([superview bounds]), CGRectGetMidY([superview bounds]));
[imageView setCenter:superCenter];
Make UIView in the center of the screen
Check center property of UIView. You can set your view center to viewControllers root view for example:
yourView.center = self.view.center;
iOS: How to Align The Center of a View With The Bottom of Another View With AutoLayout
There are a lot of ways to do this. I'll just show one of the ways.
OK, Let's do this step by step.
Step 1
First, in order to align the center of the oval image with the bottom of the rectangular image, we need to embed the rectangular view in another view in which we could name as the Container View.
This container view will have the ff attributes:
- transparent background color
- width is equal to the screen width
- height will be twice the height of the rectangular view(Later you
will know why).
For the constraints of the Container View:
- Leading is equal to super view's leading
- top space to super view is 0
- Centered horizontally in super view.
- set aspect ratio with self.
Once you are done, the constraints will look like this.
Step 2
Now for the rectangular image view, the constraints would be:
- Leading is equal to the container view's leading
- Top space to container view is 0
- Trailing is equal to container view's trailing.
Set aspect ratio with oval image view
- By setting aspect ratio with the oval image view. The change in size of this rectangular view will be proportional with the change of size of the oval view.
It will look like this:
Step 3
This time, the contraints of the oval view:
- centered horizontally in container view
- centered vertically also in container view
set aspect ratio with self
• Setting aspect ration with self ensures that when the rectangular view expands in size, the change in size of the oval view will not cause distortion.
This will be the result:
Step 4
There one very important view to add. This view will ensure that the rectangular view will always be half the size of the container view so that the center of the oval view will always be aligned with the bottom of the rectangular view. So, we call this view that we will be adding, the dummy view.
The dummy view will have the ff attributes:
- transparent background color
- width and height value should only be 1
The dummy view's contraints:
- centered vertically in the container view
- centered horizontally in the container view
- width and height are fixed.
- assign vertical spacing with rectangular view
This is how it should be done:
Alright, if you did the steps above you will achieve the effect.
This will be the overview of the constraints:
I hope this helps. :)
Proof that this works!
I ran it in the simulator... :D
iPhone 4:
iPhone 5:
iPhone 6:
iPhone 6 Plus:
How to get center of the displayed area in UIScrollView?
Try this
yourCustomAlert.center = self.view.center;
Inside your scrollView
first get visible Rect of scrollview
CGRect visibleRect = CGRectMake(myScrollView.contentOffset.x, myScrollView.contentOffset.y, myScrollView.bounds.size.width, myScrollView.bounds.size.height)
then get it's center
CGPoint centerPoint = CGPointMake(visibleRect.size.width/2, visibleRect.size.height/2);
then set your alertView's center
yourCustomAlert.center = centerPoint;
Understanding how subview.center = view.center works and why the getter and setter do different things
So when setting it, it is setting it inside it's superview. When getting the subviews center it gives you the actual views center.
So half of 50 is 25, hence 25,25. You are wanting the subview's center not its parent's center so there is no reason for it to return its parent's center coordinates, just its own.
To be a bit more technical it has to related to Frame and Bounds of a view (the getter is getting the center by using the view's bounds and the setter is using the view's frame). Here is a link that describes what those are and how they are different.
Related Topics
Perform a Segue Programmatically
Codable and Xmlparser in Swift
Swift Draw Shadow to a Uibezier Path
Wcsession Has Not Been Activated
Vapor 3 Beta Example Endpoint Request
How Are the Arkit People Occlusion Samples Being Done
Swift 3D Touch iOS 10 Home Screen Quick Actions Share Item Missing
Create a Timer Publisher Using Swift Combine
Swift Remove Object from Realm
How to Highlight a Uitextview's Text Line by Line in Swift
Swift: Dictionary Access via Index
Why Do Servicesubscribercellularproviders Return Nil? (In iOS 12)
Swift: How to Disable User Interaction While Touch Action Is Being Carried Out