How to set subviews with AutoLayout in UIScrollView programmatically?

前端 未结 4 2134
忘掉有多难
忘掉有多难 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: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

    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];
    }
    

提交回复
热议问题