Coordinates for Center of Screen for iPhone 6S [closed] - ios

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I would like to position an object in the center of my UIViewController. I have tried to use frame.width/2 and frame.height/2, for example, with a UIView:
let newView = UIView(frame: CGRect(x: frame.width/2, y: frame.height/2, width: 100, height: 100))
That method did not position the object in the center of the view as I expected. How can I position the object to the center of the view using the x-y coordinates of an iPhone 6s?

To get the actual center coordinates of the window, I'd use:
let centerX = UIScreen.mainScreen().bounds.maxX / 2
let centerY = UIScreen.mainScreen().bounds.maxY / 2
But otherwise, in whichever ViewController you're working, the center of the view is a CGPoint, the position of which can be found with:
view.center
If you just want the x and y coordinates you can get them by just adding a .x or .y after view.center.

Related

Mapbox default compassView when repositioned gives weird outcome

I have a situation where I have to reposition the map box compass view to a different location. The compassView is now rotating when I rotate the map taking some other point as its(compassView) axis and gives me a weird outcome. Screenshot is attached, the black mapBox default compass icon is rotating, refer the screenshots. Is this a MapBox sdk bug? if so, are there any work around? And tweaks? I am confused. Expert advices needed. Thanks in advance.
Hey I found another method where I will get the same result of the compass in the map box. I've placed a button and then
func mapViewRegionIsChanging(_ mapView: MGLMapView) {
compassViewUpdate(direction: Double(bearing))
inside compassViewUpdate method
func compassViewUpdate(direction:Double) {
self.compassButton.transform = CGAffineTransform(rotationAngle: CGFloat(-direction.degreesToRadians))
}
#Sarang here is a work around. Happy coding.
A built-in approach that's available in the Mapbox iOS SDK. Using these methods
to move the compass will avoid the distortion caused by .frame and .center
changes.
Generally Changing the CompassView Location
mapView.compassViewPosition = .bottomLeft
mapView.compassViewPosition = .bottomRight
mapView.compassViewPosition = .topLeft
mapView.compassViewPosition = .topRight
Use compassViewPosition to get the compass generally where you want it on the
screen.
Fine Tuning the CompassView Location
mapView.compassViewMargins = CGPoint(x: 64, y: 16)
Default x, y = 8, 8
The margin is to the outside/along the border of the view. So for .bottomLeft
the compass view will move 56 right and 8 up versus .topRight where the
compass will move 56 left and 8 down.
try changing the compassView.center instead of changing its origin or frame. I am guessing it has approximately 40 pixels width x height, and I changed to bottom left using the following:
let centerCalc = CGRect(x: 5, y: frame.height - 45, width: 40, height: 40)
compassView.center = CGPoint(x: centerCalc.midX, y: centerCalc.midY)
It should work!

How can I round only 2 corners of a button in Xcode? [duplicate]

This question already has answers here:
Round Top Corners of a UIButton in Swift
(10 answers)
Closed 5 years ago.
I have a button that is rounded in all four corners since I used the User Defined Runtime Attributes panel to make it rounded... but once running the application I saw that the button would be better off if only the right side was rounded and the left side was just squared. Is there a way I can do this? I am using Xcode 9.1 and Swift 4.
All you need is
let path = UIBezierPath(roundedRect: self.testButton.bounds, byRoundingCorners:[.topRight, .bottomRight], cornerRadii: CGSize(width: 10, height: 10))
let maskLayer = CAShapeLayer()
maskLayer.frame = self.testButton.bounds
maskLayer.path = path.cgPath
self.yourButton.layer.mask = maskLayer
O/P will look like
You said
I saw that the button would be better off if only the right side was
rounded and the left side was just squared
Did not mention clearly is both top and bottom right corner will be rounded or not so I took a liberty to imagine the same. You can choose whatever you want by modifying
[.topRight, .bottomRight]
value I specified in code above

Swift: Size vs bounds vs frame [duplicate]

This question already has answers here:
Cocoa: What's the difference between the frame and the bounds?
(12 answers)
Closed 5 years ago.
I'm making a simple game in Swift, and I was trying to find a good way to get the center of the game scene. At the moment I'm doing this:
CGPoint(x: size.width / 2, y: size.height / 2)
Seems simple enough. However, when checking, I found these four things also exist as part of the GameScene class:
self.frame
self.size
self.view.bounds
self.view.frame
I'm quite confused. Which one am I supposed to use? They all look the same to me. What's the difference?
The frame is the location in the views superview, so if it's a 100x100 square at 100 pixels from the left and the top its {100, 100, 100 100}.
The bounds is the rect, but ignoring the position in a superview. A bound-rects origin will always be 0,0. So the view in the example above would be {0,0,100,100}
Size is is just the same as bounds.size, {100,100}

Get origin of image after aspect fit [duplicate]

This question already has answers here:
How to know the image size after applying aspect fit for the image in an UIImageView
(20 answers)
Closed 6 years ago.
I want to get the CGRect value of the image after it is being "aspect fitted" onto the screen. All the solutions I found online only gives the CGSize. However, I want the origin so that I can draw a canvas on top of that image to perform drawing only on top of the image rather than the whole imageView.
Thanks,
let rect = CGRectIntegral(AVMakeRectWithAspectRatioInsideRect(imageView.image!.size, imageView.bounds))
Try to execute below code
var img=UIImageView(frame: CGRectMake(50, 50, 100, 100))
var imageOrigin=img.frame.origin //This will give you image x and y position
print(img.frame.origin) // this will print (50.0, 50.0)

Why position.y returns me wrong value? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I want implement a simple animation, filling from bottom to top. For this I do:
let waterView = UIView(frame: CGRectMake(0, 0, UIScreen.mainScreen().bounds.width, 568))
override func viewDidLoad() {
super.viewDidLoad()
print(waterView.layer.position.y)
self.waterView.backgroundColor = UIColor(red: 50/255, green: 145/255, blue: 248/255, alpha: 1)
self.view.addSubview(self.waterView)
}
override func viewDidAppear(animated: Bool) {
UIView.animateWithDuration(3.0, animations: {
print(self.waterView.layer.position.y)
})
}
And when I print it's position.y value, it returns me 284! I do not understand why??? Why it doesn't return me 0?
Is there any logic explanation?
284 is valid value.
Reason : You are setting
x=0
y = 0
widht = UIScreen.mainScreen().bounds.width(suppose 320)
height = 568
So, center will be center(x,y) = ( (320-0)/2 , (568-0)/2 ) = (160,284)
You are trying to print waterView.center.y, that's why output is 284.
Try to print waterView.frame.origin.y it will print 0.
It is because you are printing self.waterView.center.y and on calculating the height you are giving to the view which is 568, then its center is 568/2 = 284
in order to print the y coordinate of the view "waterView" then please use : print(waterView.frame.origin.y)
If you want a simple answer, position of view's layer, not exactly, but, by default indicates center of view in parent coordinate system. For example:
let v = UIView(frame: CGRect(x: 100, y: 100, width: 300, height: 300))
print(v.layer.position.y)
will print 250. It will print 0.0, if you set origin.y and height to 0.0, otherwise, formula for position.y is position.y = origin.y + height / 2
If you want origin, you need view.origin or view.layer.origin. If you want to move position to (0,0) you need to modify layer's anchorpoint, but why would you for that simple task.
BTW, don't use CGRectMake(), use initializers, as in my example.

Resources