如何使用 Swift 在屏幕中心水平或垂直设置 UIImageView,但不再有 StoryBoard [英] How to set UIImageView in the center of screen horizontally or vertically with Swift, but no more StoryBoard

查看:21
本文介绍了如何使用 Swift 在屏幕中心水平或垂直设置 UIImageView,但不再有 StoryBoard的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将 UIImageView 水平放置在屏幕的上中心,这是在 viewDidLoad( ) 中编码的.而且我有两个预先计划,这意味着我不知道指定的函数或 API.

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).我想设置一个等于屏幕宽度一半的变量.而且我知道它会与边界"或框架"有关.可悲的是,我不知道那些指定函数或参数.

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).为了确定分辨率,我想找出currentDevice.之后,我可以使用 CGRectMake((resolution.x)/2, y, length, width) 设置 UIImageView.有没有什么函数可以确认currentDevice?

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.

伊桑·乔

推荐答案

我建议使用自动布局:

    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)

此代码将每个设备中的 imageView 水平居中.我建议你研究一下 AutoLayout,这是一个不错的功能.

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

这篇关于如何使用 Swift 在屏幕中心水平或垂直设置 UIImageView,但不再有 StoryBoard的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆