简体   繁体   English

如何使用Swift在屏幕中心水平或垂直设置UIImageView,但不再使用StoryBoard

[英]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.

相关问题 Storyboard Constraints 设置 UIImageView 不超过屏幕的 50% - Storyboard Constraints set UIImageView no more than 50% of screen 如何使用自动布局在屏幕中水平和垂直居中UILabel? - How to center UILabel horizontally and vertically in screen using auto layout? swift 3:如何在UIView上对齐(垂直和水平)UIButton? - swift 3: How to center align (vertically & horizontally) UIButton on UIView? 如何使标签水平居中对齐以快速适应所有屏幕尺寸 - How to horizontally center aligning a label to fit all screen size in swift 如何滚动到UICollectionView的中心(水平和垂直方向) - How to scroll to the center of UICollectionView (Both Horizontally and Vertically) 如何在中心水平设置按钮 - how to to set button horizontally in a center 如何在Swift 3.0中以编程方式使用autolayout在UIView容器内水平居中2个或更多UIViews - How to center horizontally 2 or more UIViews inside a UIView container using autolayout programmatically in Swift 3.0 如何以编程方式在UIScrollView中垂直居中放置UIImageView? - How do I vertically center a UIImageView in a UIScrollView programmatically? 如何制作UIImageView,以编程方式在容器中垂直居中 - How to make a UIImageView, center vertically in container with constraints programmatically 在屏幕中央添加“ UIImageView” - Add `UIImageView` in the center of the screen
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM