【问题标题】:Xcode UIImageView in UIScrollView IOSUIScrollView IOS中的Xcode UIImageView
【发布时间】:2014-08-11 07:13:18
【问题描述】:

你能帮帮我吗?我想通过滚动添加背景图片,因为我的背景图片太高了。我怎样才能做到这一点?我添加 UIScrollView 并添加 UIImageView 但它不滚动。

感谢您的回复。

【问题讨论】:

    标签: xcode uiscrollview uiimageview


    【解决方案1】:

    这是工作代码:

    - (void)viewDidLoad
    {
        [super viewDidLoad];
    
        //create scrollView and set the frame to the size you want.
        //In this example the scrollView frame is the whole ViewController size
        UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
        [scrollView setBackgroundColor:[UIColor clearColor]];
    
        //create a UIImage,set the imageName to your image name
        UIImage *image = [UIImage imageNamed:@"YourImageName.png"];
        //create UIImageView and set imageView size to you image height
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, image.size.height)];
        [imageView setImage:image];
    
        //add ImageView to your scrollView
        [scrollView addSubview:imageView];
    
        //set content size of you scrollView to the imageView height
        [scrollView setContentSize:CGSizeMake(self.view.frame.size.width, imageView.frame.size.height)];
    
    
        [self.view addSubview:scrollView];
    }
    

    【讨论】:

      【解决方案2】:

      如果您希望滚动视图滚动,请将内容大小设置为大于滚动视图框架。

      【讨论】:

      • 我在 1440 上设置了 scrollView.contentSize 高度,但它总是不滚动。
      猜你喜欢
      • 2018-08-24
      • 2012-08-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-20
      • 1970-01-01
      相关资源
      最近更新 更多