iOS中,UIimage未出现在scrollview上
需要水平滚动滚动滚动视图,并在其下滚动图像。 它正在水平滚动,但即使在更改背景后也无法在上面看到我的图像。请查找以下代码:-iOS中,UIimage未出现在scrollview上,ios,objective-c,iphone,uiscrollview,Ios,Objective C,Iphone,Uiscrollview,需要水平滚动滚动滚动视图,并在其下滚动图像。 它正在水平滚动,但即使在更改背景后也无法在上面看到我的图像。请查找以下代码:- #import "ViewController.h" @interface ViewController () <UIScrollViewDelegate> @property (nonatomic , weak) IBOutlet UIScrollView *scrollView; @end @implementation ViewControlle
#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
#导入“ViewController.h”
@界面视图控制器()
@属性(非原子,弱)IBUIScrollView*scrollView;
@结束
@实现视图控制器
-(无效)viewDidLoad{
[超级视图下载];
对于(int i=0;i在循环中设置一个断点,检查ImageView的帧,并检查图像“t3”是否确实正在加载
其他你可以改变的事情
您不需要使用self.view.window.frame,只需使用self.view.frame即可
您可以将scrollview添加到视图中,但它是一个IBOutlet,如果您已经在故事板或xib的层次结构中拥有它,则不需要这样做
如果您仍然有问题,请发表意见,祝您好运。在循环中设置断点,检查图像视图的帧,并检查图像“t3”是否确实正在加载
其他你可以改变的事情
您不需要使用self.view.window.frame,只需使用self.view.frame即可
您可以将scrollview添加到视图中,但它是一个IBOutlet,如果您已经在故事板或xib的层次结构中拥有它,则不需要这样做
如果您仍然有问题,请发表评论,祝您好运。问题是关于self.view.window
根据苹果公司的文件,is说
如果视图尚未添加到窗口,则此属性为nil
您尚未将self.view
添加到窗口,因此如果使用
NSLog(@"self.view.window: %@", self.view.window);
日志将被删除
self.view.window:(空)
也就是说,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];
问题在于self.view.window
根据苹果公司的文件,is说
如果视图尚未添加到窗口,则此属性为nil
您尚未将self.view
添加到窗口,因此如果使用
NSLog(@"self.view.window: %@", self.view.window);
日志将被删除
self.view.window:(空)
也就是说,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];
非常感谢..你不需要使用self.view.window.frame,只要使用self.view.frame。(这就是问题)酷,如果可能的话,你能帮我接受我的答案,如果有任何问题请告诉我。非常感谢..你不需要使用self.view.window.frame,只要使用self.view.frame。(这就是问题)好的,如果可能的话,你能帮我接受我的答案吗?如果有任何问题,请告诉我