I'm trying to create a simple weather app and I need a simple image to scroll in a UIScrollView
but I noticed that when I set the background color to clear or transparent, the image is static in the background while a "copy" is scrolled in the UIScrollView
and since a picture is worth a thousand words, here is a proof with the default no image.
This is the hierarchy in Xcode. It's just a scrollview with an image inside it.
And this is the emulator. I scrolled a bit to the left and as you can see there is a copy of it static in the background.
If I set a background color for the scrollview it's working fine. The image can be scrolled and the copy is gone but I need it to be transparent. If it's worth mentioning, I'm running it in a virtual machine.
This is the code for adding the XIB file to the view.
[[NSBundle mainBundle] loadNibNamed:@"WeatherForecastSlideView" owner:self options:nil];
[self addSubview:self.contentView];
I hope the question is clear and sorry for typos/errors.
It turned out that there was a typo on the constructor method the init method was calling the internalInit which instatiante the nib file and add the nib hierarchy view as a subview of the class the problem is that the init method get called from code to instatiate the class and then initWithFrame get called from the framework for creating the and placing the view.
-(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;
}
so basically the change was to remove the call to internal init from the init method
-(id) init{
self = [super init];
return self;
}
and for reference this is the internalInit method
-(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;
}
the method if called more than once will add more subviews to the current view and that was causing the issue