簡體   English   中英

在屏幕中央添加“ UIImageView”

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

請先看一下代碼,

我在這里所做的是,首先我要為徽標創建一個CGRect框架。 之后,我分配了一個圖像並將其添加到子視圖中。 但是我陷入了X和Y坐標。

如果我從屏幕上獲取X坐標,則UIView框架將從坐標的中間開始。

我想做的是UIView Center和UIImageView Center應該相等,並且Logo始終保留在屏幕的中心。

要考慮的重點: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)

這將幫助您:

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