gpt4 book ai didi

ios - 我需要图像的中心在屏幕中心

转载 作者:行者123 更新时间:2023-12-01 19:59:48 26 4
gpt4 key购买 nike

无论位置如何,如何将图像固定在屏幕中央我需要在任何屏幕中间固定指南针

这是我的代码

_backgroundImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"background.jpg"]];

[self.view addSubview:_backgroundImageView];

CGPoint centerImageView = _backgroundImageView.center;
centerImageView.x = self.view.center.x;
_backgroundImageView.center = centerImageView;

但是图像固定在左侧,我不需要根据屏幕居中放置的位置

现在只有正确的位置会变化
enter image description here
现在只有正确的位置会变化
enter image description here
我需要图像的中心在屏幕中心
enter image description here

最佳答案

这是一种方法:

在viewDidLoad上:

_backgroundImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"background.jpg"]];
[self.view addSubview:_backgroundImageView];

CGRect bgImageFrame = _backgroundImageView.frame;
bgImageFrame.origin.x = (self.view.frame.size.width / 2.0) - (bgImageFrame.size.width / 2.0);
bgImageFrame.origin.y = (self.view.frame.size.height / 2.0) - (bgImageFrame.size.height / 2.0);
_backgroundImageView.frame = bgImageFrame;

那么每次屏幕旋转时都会调用此方法:
- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator {
[coordinator animateAlongsideTransition:^(id _Nonnull context) {

CGRect bgImageFrame = _backgroundImageView.frame;
bgImageFrame.origin.x = (self.view.frame.size.width / 2.0) - (bgImageFrame.size.width / 2.0);
bgImageFrame.origin.y = (self.view.frame.size.height / 2.0) - (bgImageFrame.size.height / 2.0);
_backgroundImageView.frame = bgImageFrame;

} completion:nil];
}

关于ios - 我需要图像的中心在屏幕中心,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40377868/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com