How to set UIImageView in the center of screen hor

2019-07-22 20:57发布

问题:

I am trying to put an UIImageView on the upper center of screen horizontally which is coding in the viewDidLoad( ). And I have two pre-plans, which means I do not know the specify functions or APIs.

1). I want to set a variable which equal to the half of screen's width. And I know it gonna work with something about 'bounds' or 'frame'. Pathetically, I do not know those specify function or parameters.

2). In order to make sure the resolution, I want to figure out the currentDevice. After that, I can set UIImageView with CGRectMake((resolution.x)/2, y, length, width). Is there any function can confirm the currentDevice?

And I think the first one is more efficient than the second one.

A big appreciate for your help and guidance.

Ethan Joe

回答1:

I would suggest to use autolayout:

    var imageView = UIImageView()
    imageView.setTranslatesAutoresizingMaskIntoConstraints(false)
    view.addSubview(imageView)

    // Create the constraints and add them to a Array
    var constraints = [AnyObject]()

    // This constraint centers the imageView Horizontally in the screen
    constraints.append(NSLayoutConstraint(item: imageView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0.0))

    // Now we need to put the imageView at the top margin of the view
    constraints.append(NSLayoutConstraint(item: imageView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.TopMargin, multiplier: 1.0, constant: 0.0))

    // You should also set some constraint about the height of the imageView
    // or attach it to some item placed right under it in the view such as the 
    // BottomMargin of the parent view or another object's Top attribute.
    // As example, I set the height to 500.
    constraints.append(NSLayoutConstraint(item: imageView, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 500.0))

    // The next line is to activate the constraints saved in the array
    NSLayoutConstraint.activateConstraints(constraints)

This code horizontally centers the imageView in every device. I suggest you investigating AutoLayout, it's a nice feature.

Here's a nice link where you can start with it: Learning to love Auto Layout... Programmatically



回答2:

So, the best solution to this would be to utilize auto layout. Say your UIImageVew is defined as var imageView = UIImageView!. Then you would do the following.

var centerXConst = NSLayoutConstraint(item: imageView, attribute: .CenterX, relatedBy: .Equal, toItem: self.view, attribute: .CenterX, multiplier: 1, constant: 1)
var centerYConst = NSLayoutConstraint(item: imageView, attribute: .CenterY, relatedBy: .Equal, toItem: self.view, attribute: .CenterY, multiplier: 1, constant: 1)

self.view.addLayoutConstraints([centerXConst, centerYConst])

What you are doing is setting the center of the imageView on the x and y axis respectively to the same as the center of the view on the x and y axis respectively.

To find the width of the view as you proposed in 1), do the following:

var screenWidth = self.view.bounds.width