objective-c - UiScrollview 内容在后台重复
问题描述
我正在尝试创建一个简单的天气应用程序,我需要一个简单的图像来滚动,UIScrollView
但我注意到,当我将背景颜色设置为清晰或透明时,图像在背景中是静态的,而“副本”是滚动的并且UIScrollView
由于一张图片值一千字,这里有一个默认没有图片的证明。
这是 Xcode 中的层次结构。它只是一个带有图像的滚动视图。
这就是模拟器。我向左滚动了一下,你可以看到它的副本在背景中是静态的。
如果我为滚动视图设置背景颜色,它工作正常。图像可以滚动,副本消失了,但我需要它是透明的。如果值得一提,我正在虚拟机中运行它。
这是将 XIB 文件添加到视图的代码。
[[NSBundle mainBundle] loadNibNamed:@"WeatherForecastSlideView" owner:self options:nil];
[self addSubview:self.contentView];
我希望这个问题很清楚,对错别字/错误感到抱歉。
解决方案
事实证明,构造函数方法有一个错字,init方法正在调用instatiante nib文件并将nib层次结构视图添加为类的子视图,问题是从代码中调用init方法来实例化类,然后从框架调用 initWithFrame 以创建和放置视图。
-(id) init{
self = [super init];
if (self) {
[self internalInit];
}
}
-(id) initWithCoder:(NSCoder *)aDecoder{
self = [super initWithCoder:aDecoder];
if (self) {
[self internalInit];
}
return self;
}
-(id) initWithFrame:(CGRect)frame{
self = [super initWithFrame:frame];
if (self) {
[self internalInit];
}
return self;
}
所以基本上改变是从 init 方法中删除对内部 init 的调用
-(id) init{
self = [super init];
return self;
}
作为参考,这是 internalInit 方法
-(void)internalInit{
[[NSBundle mainBundle] loadNibNamed:@"WeatherForecastSlideView" owner:self options:nil];
[self addSubview:self.contentView];
self.scrollView.delegate = self;
self.translatesAutoresizingMaskIntoConstraints = NO;
NSLayoutConstraint* height = [self.heightAnchor constraintEqualToConstant:self.contentView.bounds.size.height];
height.active = YES;
height.priority = UILayoutPriorityDefaultHigh;
NSLayoutConstraint* width = [self.widthAnchor constraintEqualToConstant:self.contentView.bounds.size.width];
width.active = YES;
width.priority = UILayoutPriorityDefaultHigh;
}
如果多次调用该方法,则会向当前视图添加更多子视图,这会导致问题
推荐阅读
- caching - 使用 Redis 缓存 rss 提要 - 几 kb 到几 MB
- flutter - DART:如何在单击按钮以显示在下一页上时随机生成六 (6) 位数字?
- python - 如何在这个开源手风琴中放置垂直滚动条?
- vba - 在组合框更新控件后运行 AfterUpdate
- asp.net - 在任何 gridview 事件中,asp.net 中的排序都会丢失
- python - 通过vscode的SSH隧道打开一个Matplotlib图
- python - 从 python 创建容器时,Docker API 无法识别“npipe”挂载类型
- firebase - 如何使用来自 Firebase 存储的图片设置图像视图
- c# - 在 C# 中为依赖解析器创建延迟缓存值?
- windows - 如何在子目录中使用 for 循环运行应用程序