[英]How to set UIImageView in the center of screen horizontally or vertically with Swift, but no more StoryBoard
I am trying to put an UIImageView
on the upper center of screen horizontally which is coding in the viewDidLoad( )
. 我试图在屏幕的上部中心水平放置一个
UIImageView
,它在viewDidLoad( )
编码。 And I have two pre-plans, which means I do not know the specify functions or APIs. 我有两个预先计划,这意味着我不知道指定函数或API。
1). 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). 2)。 In order to make sure the resolution, I want to figure out the
currentDevice
. 为了确保分辨率,我想弄清楚
currentDevice
。 After that, I can set UIImageView
with CGRectMake((resolution.x)/2, y, length, width)
. 之后,我可以使用
CGRectMake((resolution.x)/2, y, length, width)
设置UIImageView
。 Is there any function can confirm the currentDevice
? 有没有任何功能可以确认
currentDevice
?
And I think the first one is more efficient than the second one. 我认为第一个比第二个更有效。
A big appreciate for your help and guidance. 非常感谢您的帮助和指导。
Ethan Joe 伊桑乔
I would suggest to use autolayout: 我建议使用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. 此代码在每个设备中将imageView水平居中。 I suggest you investigating AutoLayout, it's a nice feature.
我建议你调查一下AutoLayout,这是一个不错的功能。
Here's a nice link where you can start with it: Learning to love Auto Layout... Programmatically 这是一个很好的链接,你可以从它开始: 学习爱自动布局...编程
So, the best solution to this would be to utilize auto layout. 因此,最好的解决方案是利用自动布局。 Say your
UIImageVew
is defined as var imageView = UIImageView!
假设您的
UIImageVew
被定义为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. 你正在做的是分别在x和y轴上将imageView的中心设置为与x和y轴上的视图中心相同。
To find the width of the view as you proposed in 1), do the following: 要查找1)中建议的视图宽度,请执行以下操作:
var screenWidth = self.view.bounds.width
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.