How to set subviews with AutoLayout in UIScrollView programmatically?

前端 未结 4 2106
忘掉有多难
忘掉有多难 2020-12-29 10:17

I am creating app with UiScrollview and UIPagectontrol using Autolayout Programmatically, for

I have Created TKScroller as subclass of UIView, I am init it

相关标签:
4条回答
  • 2020-12-29 11:04

    In my experience with AutoLayout (that I use programmatically too). You should not add views directly to your UIScrollView.

    You should do one UIView and only one, that you add as subview of your UIScrollView.

    Once this is done, you can add all of your subviews to this UIView.

    Everything is explained in detail here : Apple iOS Technical Note TN2154

    0 讨论(0)
  • 2020-12-29 11:07

    If you want your TKScroller fixed to the center and support orientation changes, you can simply put the following code in your init

    self.autoresizingMask = UIViewAutoresizingFlexibleWidth;
    

    and you may need to check all your subview's of TKScroller. If you don't like autosize , you can set (one of the three)

    self.autoresizingMask = UIViewAutoresizingFlexibleRightMargin;
    self.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
    self.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin;
    
    0 讨论(0)
  • 2020-12-29 11:08

    I know that the problem has been solved. and i hope that this solution my help for some people so i am posting it.

    Below code is for adding the scrollview to the uiviewcontroller

      self.scrollView = UIScrollView()
        self.scrollView.backgroundColor = UIColor.greenColor()
        self.scrollView.setTranslatesAutoresizingMaskIntoConstraints(false)
        self.scrollView.scrollEnabled=true
        self.scrollView.pagingEnabled = true
        self.view.addSubview(self.scrollView)
    
        self.view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|[scrollView]|", options: NSLayoutFormatOptions(0), metrics: nil, views: ["scrollView":self.scrollView]))
    
        self.view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|[scrollView]|", options: NSLayoutFormatOptions(0), metrics: nil, views: ["scrollView":self.scrollView]))
    

    After added the scrollview and add the subviews/images like this(this is an example for images showing in full size of scrollview like in photos app )

    var prev:UIImgeView?
    
            for var index = 0; index < self.images.count; ++index {
                var iview = UIImgeView()
                iview.setTranslatesAutoresizingMaskIntoConstraints(false)
                self.scrollView.addSubview(iview)
                var image = self.images.objectAtIndex(index) as UIImage
                iview.image = image
                if (index == 0){
                    self.scrollView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|[iview(==scrollView)]", options: NSLayoutFormatOptions(0), metrics: nil, views: ["iview":iview,"scrollView":self.scrollView]))
                    prev = iview
    
                }
                else{
                    self.scrollView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:[prev][iview(==prev)]", options: NSLayoutFormatOptions(0), metrics: nil, views: ["iview":iview,"prev":prev!]))
                    prev = iview
                }
                self.scrollView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|[iview(==scrollView)]|", options: NSLayoutFormatOptions(0), metrics: nil, views: ["iview":iview,"scrollView":self.scrollView]))
    
                if (index == self.images.count-1){
                    self.scrollView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:[prev]|", options: NSLayoutFormatOptions(0), metrics: nil, views: ["prev":prev!]))
    
                }
    
            }
    
    0 讨论(0)
  • 2020-12-29 11:11

    Here is a SO answer has explained how to do this with auto layout, he has explain perfectly , In here there is vertically textfields are there But in your case it is you have to set Horizontal views constraints.

    Alternative

    Rather that setting constraints you can set just frame of the subview and set it in Scrollview, And based on orientation you can change frames of the scrolview's subviews.

    Your setData Method like,

    -(void)setData{
    
        [self layoutIfNeeded];
        CGRect mainFrame=scrollView.frame;
        CGRect frame;
        for (int i=0; i<arrayData.count ; i++)
        {
            CGRect frame;
            frame.origin.x = scrollView.frame.size.width * i;
            frame.origin.y = 0;
            frame.size = scrollView.frame.size;
    
            frame.origin=CGPointMake(0, 0);
            UIView *subview = [[UIView alloc]initWithFrame:frame];
            subview.backgroundColor = [self getRandomColor];
            [scrollView addSubview:subview];
        }
        scrollView.contentSize = CGSizeMake(scrollView.frame.size.width * arrayData.count, mainFrame.size.height/2);
    }
    

    Now you using NSNotificationCenter you can get notify when device orientation chaged, so in this selector method of it call your setData method,

    [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(setData)
                                                     name:UIDeviceOrientationDidChangeNotification
                                                   object:nil];
    

    Now in you setData method you need remove all subviews because when device changes Orientation it will add new views to your scrollview, so remove all subview from Scrollview before setting its frame,

            [scrollView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    

    Make sure you are removing observer from your class like,

    - (void)dealloc
    {
        [[NSNotificationCenter defaultCenter] removeObserver:self];
    }
    
    0 讨论(0)
提交回复
热议问题