gpt4 book ai didi

iphone - ImageView 在设备方向更改时消失 - iPhone - objective-c

转载 作者:塔克拉玛干 更新时间:2023-11-02 10:16:21 26 4
gpt4 key购买 nike

当设备在 iPhone 上的方向发生变化时,我正在学习如何更改 UI 布局,我遇到了一个问题..

我有一个主从应用程序,如果我在详细 View 中更改方向,一切正常,但如果我在主视图 Controller 中更改它,然后推送详细 View ,则一切正常,除了我的 ImageView 未显示..

相关代码如下:

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
if (UIDeviceOrientationIsLandscape(orientation)){
[self layoutLandscape];
}
else if (UIDeviceOrientationIsPortrait(orientation)){
[self layoutPortrait];
}

//set up UI elements
nameLabel.text = name;
dateOfBirthLabel.text = dateOfBirth;
numberOfVotesLabel.text = numberOfVotes;
reasonForFameLabel.text = reasonForFame;
numberOfStarsLabel.text = numberOfStars;
[indicator startAnimating];
//Perform this method in background (gets the product image from URL)
[self performSelectorInBackground:@selector(loadImage) withObject:nil];

//Gives UIImageView memeThumb a orange border
//Must import the QuartsCore Framework for this to work
[imageView.layer setBorderColor: [[UIColor blackColor] CGColor]];
[imageView.layer setBorderWidth: 2.0];
}

- (void) layoutLandscape{
imageView.frame = CGRectMake(20, 20, 200, 150);
indicator.frame = CGRectMake(150, 40, 20, 20);

nameMarker.frame = CGRectMake(70, 115, 51, 150);
dateOfBirthMarker.frame = CGRectMake(20, 135, 101, 180);
numberOfVotesMarker.frame = CGRectMake(233, 10, 134, 30);
reasonForFameMarker.frame = CGRectMake(230, 60, 137, 30);
numberOfStarsMarker.frame = CGRectMake(236, 110, 130, 30);

nameLabel.frame = CGRectMake(125, 175, 135, 30);
dateOfBirthLabel.frame = CGRectMake(125, 210, 135, 30);
numberOfVotesLabel.frame = CGRectMake(372, 10, 134, 30);
reasonForFameLabel.frame = CGRectMake(372, 60, 135, 30);
numberOfStarsLabel.frame = CGRectMake(372, 110, 135, 30);
}

- (void) layoutPortrait{
imageView.frame = CGRectMake(20, 20, 280, 197);
indicator.frame = CGRectMake(150, 109, 20, 20);

nameMarker.frame = CGRectMake(106, 225, 51, 30);
dateOfBirthMarker.frame = CGRectMake(56, 263, 101, 30);
numberOfVotesMarker.frame = CGRectMake(23, 301, 134, 30);
reasonForFameMarker.frame = CGRectMake(20, 339, 137, 30);
numberOfStarsMarker.frame = CGRectMake(27, 377, 130, 30);

nameLabel.frame = CGRectMake(165, 225, 135, 30);
dateOfBirthLabel.frame = CGRectMake(165, 263, 135, 30);
numberOfVotesLabel.frame = CGRectMake(165, 301, 134, 30);
reasonForFameLabel.frame = CGRectMake(165, 339, 135, 30);
numberOfStarsLabel.frame = CGRectMake(165, 377, 135, 30);
}

- (void) loadImage {
NSURL *imageURL = [NSURL URLWithString:URL];
NSData *data = [NSData dataWithContentsOfURL:imageURL];
UIImage *image = [UIImage imageWithData:data];
imageView.image = image;
[indicator stopAnimating];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation{
UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
if (UIDeviceOrientationIsLandscape(orientation)) {
[self layoutLandscape];
}
else if (UIDeviceOrientationIsPortrait(orientation)) {
[self layoutPortrait];
}
}

有人有什么想法吗?

最佳答案

如果 View 已经创建并驻留在内存中,则不会调用 viewDidLoad。

尝试将您的代码移动到

-(void) viewWillAppear
{
UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
if (UIDeviceOrientationIsLandscape(orientation)){
[self layoutLandscape];
}
else if (UIDeviceOrientationIsPortrait(orientation)){
[self layoutPortrait];
}
}

关于iphone - ImageView 在设备方向更改时消失 - iPhone - objective-c ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9861108/

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