gpt4 book ai didi

ios - 移动一个框架的父大小的一半将它一直移动(坐标加倍)?

转载 作者:行者123 更新时间:2023-11-28 22:01:53 24 4
gpt4 key购买 nike

我不知道发生了什么。

我没有做任何特别的事情。

我有一个以编程方式创建的 UIViewController,没有 Storyboard。

然后我创建一个没有什么特别的 UIView

#import "SettingsView.h"
@interface SettingsView()
@property UIImageView* bg;
@end
@implementation SettingsView

- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
self.bg = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"happy.png"]];
[self addSubview:self.bg];
}
return self;
}

-(void)layoutSubviews
{
self.bg.frame = self.frame;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
// Drawing code
}
*/

@end

我在 VC 中实例化它,当我想显示它时:

-(void)showSettingsView
{
self.settings.frame = self.view.frame;
self.btnInfo.userInteractionEnabled = NO;
self.btnPause.userInteractionEnabled = NO;
self.btnSettings.userInteractionEnabled = NO;
self.view.userInteractionEnabled = NO;
[self.view addSubview:self.settings];
CGRect frame = self.settings.frame;
frame.origin.x = frame.size.width * 0.48;
frame.origin.y = 0;
self.settings.frame = frame;
}

它不是在屏幕的大约一半处看到 ImageView ,而是从屏幕的两倍 (0.48 * 2 = 96%) 处开始。

我不明白为什么坐标要加倍之类的?

当我检查框架时,我清楚地看到原点在 150 左右,这是 iPhone 宽度的一半。

会发生什么?

谢谢

最佳答案

难道是在……

-(void)layoutSubviews
{
self.bg.frame = self.frame;
}

您应该使用 bounds 而不是 self 对象的 frame...

-(void)layoutSubviews
{
self.bg.frame = self.bounds;
}

关于ios - 移动一个框架的父大小的一半将它一直移动(坐标加倍)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24920268/

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