• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

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

[复制链接]
菜鸟教程小白 发表于 2022-12-13 12:36:53 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

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

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

下面是相关代码:

- (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 performSelectorInBackgroundselector(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)shouldAutorotateToInterfaceOrientationUIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

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

有人有什么想法吗?



Best Answer-推荐答案


如果 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/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap