【问题标题】:UIimage not appearing over scrollview in iOSUIimage没有出现在iOS中的滚动视图上
【发布时间】:2016-07-16 20:01:46
【问题描述】:

需要水平滚动滚动视图并在其下添加图像。 它正在水平滚动,但即使在更改背景后也看不到我的图像。请在下面找到代码:-

#import "ViewController.h"

@interface ViewController () <UIScrollViewDelegate>

@property (nonatomic , weak) IBOutlet UIScrollView *scrollView;

@end

@implementation ViewController

   - (void)viewDidLoad {
[super viewDidLoad];

for(int i=0; i<5; i++) {
    UIImageView *imageView = [[UIImageView alloc]  initWithFrame:CGRectMake((self.view.window.frame.size.width/5)*i, 0, self.view.window.frame.size.width/5, 48)];
    imageView.image = [UIImage imageNamed:@"t3"];
    imageView.backgroundColor = [UIColor blackColor];
    [_scrollView addSubview:imageView];
}

_scrollView.contentSize = CGSizeMake(self.view.window.frame.size.width, 48.0f);
[self.view addSubview:_scrollView];

}

- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
      }

@end

【问题讨论】:

    标签: ios objective-c iphone uiscrollview


    【解决方案1】:

    在循环中设置断点并检查图像视图的帧并检查图像“t3”是否实际正在加载。

    您可以更改的其他内容,

    1. 不需要使用self.view.window.frame,使用self.view.frame即可。
    2. 您将滚动视图添加到您的视图中,但它是一个 IBOutlet,如果您已经在情节提要或 xib 的层次结构中拥有它,则不需要这样做。

    如果还有问题请留言,祝你好运。

    【讨论】:

    • 非常感谢..您不需要使用self.view.window.frame,只需使用self.view.frame。(这是问题)
    • 酷,如果可能的话,你能帮忙接受我的回答吗,如果有任何问题,请告诉我。
    【解决方案2】:

    问题是关于self.view.window

    从 Apple 的文档中可以看出

    如果视图尚未添加到窗口,则此属性为 nil

    你还没有将self.view添加到窗口中,所以如果你使用

    NSLog(@"self.view.window: %@", self.view.window);
    

    日志将是

    self.view.window: (null)

    也就是说self.view.window.frame.size.width的值为0。


    所以解决方法很简单:

    使用self.view.frame.size.width 而不是self.view.window.frame.size.width

    使用下面的代码将self.view 添加到窗口

    [[[[UIApplication sharedApplication] windows] firstObject] addSubview:self.view]; 
    

    【讨论】:

    • thanx mate.. 很好的解释
    猜你喜欢
    • 2020-06-10
    • 2014-08-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-11
    相关资源
    最近更新 更多