今天做多线程的课程代码,是一个进度条显示下载进度。
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[[SDImageCache sharedImageCache] clearDisk];
UIView * progressView = [[UIView alloc] init];
self.progressView = progressView;
[self.imageView addSubview:progressView];
NSLog(@"%f",[UIScreen mainScreen].bounds.size.width);
NSLog(@"%f",self.imageView.bounds.size.width);
progressView.backgroundColor = [UIColor redColor];
CGRect frame =progressView.frame;
[self.imageView sd_setImageWithURL:[NSURL URLWithString:@"http://cdn.wall88.com/51b260599c9b026922.jpg"] placeholderImage:nil options:SDWebImageProgressiveDownload progress:^(NSInteger receivedSize, NSInteger expectedSize) {
float progress = (float)receivedSize/expectedSize;
//progressView.frame = CGRectMake(frame.origin.x, frame.origin.y, frame.size.width * progress,frame.size.height);
} completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
}];
}
进度条是一个uiview,通过改变它的宽度,来达到显示进度。
但是我的进度条超出了ImageView的范围,比它长。
原因是在storyboard 中我设置控制器view是SizesClasses,autolayout
这时候不应该直接设置frame,而应该使用约束。
取消SizesClasses,autolayout 后正常显示。
但是我想知道SizesClasses,autolayout 开着,有没有办法做到呢?
我试着打印self.imageView.bounds.size.width,因为进度条的宽度是按着这个值设置的。在viewdidload中,打印是568,而屏幕只有375
这是因为开了sizeClasses吧。然后在ViewdidAppear和viewDidLayoutSubviews中打印,是343.000000,正常了,说明,在Viewdidload时子View还没有自适应屏幕。
但是我在viewDidLayoutSubviews中设置进度条frame已经没有意义了,因为VIEW刚加载完成,图片就开始下载了。
总结:SizesClasses,autolayout开启后不能用frame,不准确,在viewDidLayoutSubviews后控件的大小应该适应屏幕了