简体   繁体   English

在屏幕中央添加“ UIImageView”

[英]Add `UIImageView` in the center of the screen

Please take a look at the code first, 请先看一下代码,

What I am doing here is that first I am creating a CGRect frame for the logo. 我在这里所做的是,首先我要为徽标创建一个CGRect框架。 After that, I have assigned an Image and added it to the subview. 之后,我分配了一个图像并将其添加到子视图中。 But I stuck in the X and Y coordinates. 但是我陷入了X和Y坐标。

IF I take X coordinates from the screen then the UIView Frame will start from the Mid of the coordinate. 如果我从屏幕上获取X坐标,则UIView框架将从坐标的中间开始。

What I wanted to do is that the UIView Center and UIImageView Centre Should be equal and Logo will always remain in the centre of the screen. 我想做的是UIView Center和UIImageView Center应该相等,并且Logo始终保留在屏幕的中心。

Important Points to Consider: 1. I am not adding anything through drag and Drop. 要考虑的重点:1.我没有通过拖放添加任何内容。

nfloat X = ScreenDimension.Size.Width;
nfloat Y = ScreenDimension.Size.Height;

CGRect LogoFrame = new CGRect(X/2,Y/2,300,300);

UIImageView Logo = new UIImageView(LogoFrame);
var ImageFile = UIImage.FromFile("Images/Logo.png");

Logo.Layer.Contents = ImageFile.CGImage;
SubView.AddSubview(Logo);

Logo.CenterXAnchor.ConstraintEqualTo(SubView.CenterXAnchor).Active = true; (Ignore this)

This will help you:- 这将帮助您:

let imageView = UIImageView()

self.view.addSubview(imageView)     

imageView.center = CGPoint(x: self.view.frame.size.width/2, y: 
self.view.frame.size.height/2)

imageView.autoresizingMask = [.flexibleWidth, .flexibleHeight]

imageView.image = UIImage(named: "imageName")

imageView.contentMode = .scaleAspectFit    

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM