@interface ViewController () @property (weak, nonatomic) IBOutlet UIScrollView *scrollView; @property (weak, nonatomic) IBOutlet UIImageView *imageView; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. //告诉scrollview内部内容的实际大小 self.scrollView.contentSize=CGSizeMake(892, 632); } @end
2.2 Swift模式下(ViewController.swift)
1 2 3 4 5 6 7 8 9 10 11 12
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var scrollView: UIScrollView! @IBOutlet weak var imageView: UIImageView! override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view. self.scrollView.contentSize=CGSize.init(width:892, height: 632); } }
-(void)setImage:(UIImage *)image { _image=image; self.imageView.image=image; [self.imageView sizeToFit]; self.scrollView.contentSize=image.size; } -(UIImageView *)imageView { if(_imageView==nil) { _imageView=[[UIImageView alloc]init]; [self.scrollView addSubview:_imageView]; } return _imageView; } - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. //告诉scrollview内部内容的实际大小 self.image=[UIImage imageNamed:@"minion"]; }
@end
3.2 Swift模式下(ViewController.swift)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var scrollView: UIScrollView! var imageView: UIImageView? var image:UIImage?; override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view. image=UIImage.init(named: "minion"); imageView=UIImageView.init() imageView?.image=image; imageView?.sizeToFit() scrollView.addSubview(imageView!); scrollView.contentSize=image?.size as! CGSize } }