I want the image in my UIImageView to us a different image file that's cropped to correctly fill the landscape mode upon the orientation changing from portrait to landscape. All of the following code is defined in a class that extends UIViewController.
The following function is definitely being called when the phone rotates because my printf function is printing to the output.
- (void) traitCollectionDidChange: (UITraitCollection *) previousTraitCollection {[super traitCollectionDidChange: previousTraitCollection]; if ((self.traitCollection.verticalSizeClass != previousTraitCollection.verticalSizeClass) || (self.traitCollection.horizontalSizeClass != previousTraitCollection.horizontalSizeClass)) { printf("Orientation Change!\n"); UIImage * newImage = [UIImage imageNamed: @"landscape-image"]; [self.imageView setImage:newImage]; }
}
I defined the property imageView in my .h file as follows:
@property (nonatomic, nonnull, readonly) UIImageView * imageView;
And I initialize the imageView as follows:
UIImage * image = [UIImage imageNamed:@“portrait-image”];
UIImageView * _imageView = [[UIImageView alloc] initWithImage:image];
_imageView.contentMode = UIViewContentModeScaleAspectFill;
_imageView.translatesAutoresizingMaskIntoConstraints = NO;
[pageView addSubview:_imageView];
This does not work for me though. When I change the orientation, the imageView image stays the same and zooms in like it normally does. Since the traitCollectionDidChange function is being called when the phone rotates, I assume the issue must be with how I’m changing the image. I’m relatively new to iOS development so I could just be missing something important for updating UIImageViews. Any help is appreciated.
#ios #objective-c #xcode