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

编程入门 行业动态 更新时间:2024-10-27 17:21:55
本文介绍了如何使用 Swift 在屏幕中心水平或垂直设置 UIImageView,但不再有 StoryBoard的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我试图将 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屋!

更多推荐

[db:关键词]

本文发布于:2023-04-29 03:44:35,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1183977.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:但不   如何使用   屏幕   水平   中心

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!