简体   繁体   中英

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. After that, I have assigned an Image and added it to the subview. But I stuck in the X and Y coordinates.

IF I take X coordinates from the screen then the UIView Frame will start from the Mid of the coordinate.

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.

Important Points to Consider: 1. I am not adding anything through drag and Drop.

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    

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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