scrollView doesn't work even though contentSiz

2019-03-04 02:20发布

    - (void)viewDidLoad
{

[super viewDidLoad];




self.navigationController.navigationBar.translucent = YES;
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"sprites_0001_Rectangle-1.png"]  forBarMetrics:UIBarMetricsDefault];



self.navigationController.navigationBar.translucent = YES;

UIImageView *imageView = [[UIImageView alloc]init];
UIImage *image = [UIImage imageNamed:@"sprites_0000s_0008_1st-Page.png"];
imageView.image = image;


//imageView.translatesAutoresizingMaskIntoConstraints = NO;
imageView.backgroundColor = [UIColor clearColor];
self.view = imageView;



if ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad) {

     //Write the code to set up view for iPad

   }else{

    UIScrollView *scrollView = [[UIScrollView alloc] init];
    scrollView.backgroundColor = [UIColor clearColor];
    scrollView.translatesAutoresizingMaskIntoConstraints = NO;

    [self.view addSubview:scrollView];
    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-40-[scrollView(==240)]"
                                                                      options:0
                                                                      metrics:nil
                                                                        views:NSDictionaryOfVariableBindings(scrollView)]];
    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-50-[scrollView(==468)]"
                                                                      options:0
                                                                      metrics:nil
                                                                        views:NSDictionaryOfVariableBindings(scrollView)]];

    UIImageView *imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"ParentsMock.png"]];
    imageView.translatesAutoresizingMaskIntoConstraints = NO;
    [scrollView addSubview:imageView];



    [scrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"|[imageView(==1000)]|"
                                                                       options:0
                                                                       metrics:0
                                                                         views:NSDictionaryOfVariableBindings(imageView)]];
    [scrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[imageView(==2000)]|"
                                                                       options:0
                                                                       metrics:0
                                                                         views:NSDictionaryOfVariableBindings(imageView)]];

    }

This is my attempt to create UIScrollView programmatically however I couldn't make the scrollView work even though I set the contentSize after adding the subview into the scrollView.

How it looks like

As you can see in this picture, I use UINavigationController to wrap a UIViewControllar and set UIImageView as its view. the I created a scrollView and add it on top of the view. then I create another imageView1 and insert it into the scrollView.

please note that the view of the entire view controller is an imageView which is different from the imageView i insert into the scrollView.

6条回答
我只想做你的唯一
2楼-- · 2019-03-04 02:42

You should set your content size using constraints, ton by setting it directly. It does not scroll because you don't have constraints to the left, top, right or bottom of your scroll view content. See my answer here

Edit:

Try adding this:

    imageView.translatesAutoresizingMaskIntoConstraints = NO;
    [scrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:
                                                         @"|[imageView(==1000)]|"
                                                        options:0
                                                        metrics:0
                                               views:NSDictionaryOfVariableBindings(imageView)]];
    [scrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:
                                                         @"V:|[imageView(==2000)]|"
                                                        options:0
                                                        metrics:0
                                               views:NSDictionaryOfVariableBindings(imageView)]];

and remove the code where you are setting the content size.

Edit:

Replace your entire code with this:

        UIScrollView *scrollView = [[UIScrollView alloc] init];
        scrollView.backgroundColor = [UIColor clearColor];
        scrollView.translatesAutoresizingMaskIntoConstraints = NO;

        [self.view addSubview:scrollView];
        [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-40-[scrollView(==240)]"
                                                                          options:0
                                                                          metrics:nil
                                                                            views:NSDictionaryOfVariableBindings(scrollView)]];
        [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-50-[scrollView(==468)]"
                                                                          options:0
                                                                          metrics:nil
                                                                            views:NSDictionaryOfVariableBindings(scrollView)]];

        UIImageView *imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"ParentsMock.png"]];
        imageView.translatesAutoresizingMaskIntoConstraints = NO;
        [scrollView addSubview:imageView];



        [scrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"|[imageView(==1000)]|"
                                                                           options:0
                                                                           metrics:0
                                                                             views:NSDictionaryOfVariableBindings(imageView)]];
        [scrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[imageView(==2000)]|"
                                                                           options:0
                                                                           metrics:0
                                                                             views:NSDictionaryOfVariableBindings(imageView)]];
查看更多
乱世女痞
3楼-- · 2019-03-04 02:44

The only way I have been successful in creating UIScrollViews is to use Autolayout. This is the simplest and most effective way I know. For this you will need to remove the

initWithFrame:CGRectMake(40, 100, 240, 168)];
查看更多
虎瘦雄心在
4楼-- · 2019-03-04 02:52

Add the delegate to self like this:

 UIScrollView *scrollView = [[UIScrollView alloc]init];

scrollView.delegate=self;

 UIImageView *imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"ParentsMock.png"]];
 [scrollView addSubview:imageView];
查看更多
放我归山
5楼-- · 2019-03-04 03:00

I found out the answer. It seems that I need to enable userInteraction on UIImageView. Remember when I use UIImageView as the view of the view controller. Therefore default value for userInteractionEnabled Property of UIImageView is NO. It ACCEPTS NO touches at all. We need we enable it like this

imageView.userInteractionEnabled = YES;

PROBLEM Solved.

查看更多
走好不送
6楼-- · 2019-03-04 03:01

Replace:

NSDictionary *layoutDic = ....

With:

NSDictionary *layoutDic = NSDictionaryOfVariableBindings(scrollView);

Edit: I use your code, and it works, I can scroll, I do in viewDidLoad

UIScrollView *scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(40, 100, 240, 168)];

UIImageView *imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"purple_button.png"]];
[scrollView addSubview:imageView];
scrollView.contentSize = CGSizeMake(1000, 2000);
scrollView.backgroundColor = [UIColor clearColor];
scrollView.scrollEnabled = YES;
scrollView.translatesAutoresizingMaskIntoConstraints = NO;
NSDictionary *layoutDic = NSDictionaryOfVariableBindings(scrollView);
[self.view addSubview:scrollView];
NSArray *scrollViewConstraintWidth = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-40-[scrollView(==240)]" options:0 metrics:nil views:layoutDic];
NSArray *scrollViewConstraintHeight = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-50-[scrollView(==468)]" options:0 metrics:nil views:layoutDic];
[self.view addConstraints:scrollViewConstraintHeight];
[self.view addConstraints:scrollViewConstraintWidth];
查看更多
做自己的国王
7楼-- · 2019-03-04 03:06

in properties under Show the file inspector uncheck Use Auto Layout

查看更多
登录 后发表回答