uiimageview - Landscape views only loading properly from portrait and after rotating, why? -
new ios developer here. have multiple views require different images displayed in portrait , landscape. have implemented , portrait image loads fine, and, upon rotation, landscape image loads fine. however, if device in landscape orientation switches view, loads improperly - wrong size, resolution, alignments, etc. code dealing orientation changes below:
- (void)didrotatefrominterfaceorientation:(uiinterfaceorientation)frominterfaceorientation { if((self.interfaceorientation == uideviceorientationlandscapeleft) || (self.interfaceorientation == uideviceorientationlandscaperight)) { _image1.image = [uiimage imagenamed:@"landscape.png"]; } else if((self.interfaceorientation == uideviceorientationportrait) || (self.interfaceorientation == uideviceorientationportraitupsidedown)) { _image1.image = [uiimage imagenamed:@"portrait.png"]; } }
i believe because method called upon rotation. if rotate improper, initial landscape view, instance, displays correct images once again. there way method run , load proper landscape view when initial orientation in landscape? or way force correct image display? much.
i fixed issue adding orientation checker. added following in .h:
@property (nonatomic, readonly) uideviceorientation *orientation;
then added .m file in viewdidload method:
if(([[uidevice currentdevice] orientation] == uideviceorientationlandscapeleft) || ([[uidevice currentdevice] orientation] == uideviceorientationlandscaperight)) { _image1.image = [uiimage imagenamed:@"landscape.png"]; }
this checks if initial orientation landscape. if is, loads landscape.png image. otherwise, since default image portrait.png, set in storyboard, loads if orientation in portrait. cheers!
edit: above code not advised can run issues when using it, such orientation-locked devices. changed check status bar's orientation, rather device's orientation, below:
if(([[uiapplication sharedapplication] statusbarorientation] == uiinterfaceorientationlandscapeleft) || ([[uiapplication sharedapplication] statusbarorientation] == uiinterfaceorientationlandscaperight)) { _image1.image = [uiimage imagenamed:@"landscape.png"]; }
you not need declare variables in .h, , add above in viewdidload method.
Comments
Post a Comment