自动布局ScrollView以编程方式快速

时间:2018-11-07 09:39:42

标签: swift xcode uiscrollview autolayout

我需要使用自动布局设置全屏ScrollView的布局。目前,我有此代码,但没有全高教派,我不知道要更改哪个变量。我有这个代码

func setControlsType(controlsType: ControlsType, bounds: CGRect, startingDim: CGFloat, scrolledDim: CGFloat) {
    self.controlsType = controlsType
    if Utils.isInPortraitState() {
        /*self.startingFrame = CGRect(x: 0, y: bounds.height - startingDim, width: bounds.width, height: scrolledDim)
        self.scrolledFrame = CGRect(x: 0, y: bounds.origin.y + bounds.height - scrolledDim, width: bounds.width, height: scrolledDim)*/
        self.startingFrame = CGRect(x: 0, y: bounds.height, width: bounds.width, height: scrolledDim)
        self.scrolledFrame = CGRect(x: 0, y: bounds.origin.y + bounds.height, width: bounds.width, height: scrolledDim)
    } else {
        self.startingFrame = CGRect(x: startingDim - scrolledDim, y: 0, width: scrolledDim, height: bounds.height)
        self.scrolledFrame = CGRect(x: 0, y: 0, width: scrolledDim, height: bounds.height)
    }


func setControls(type: ControlsType) {
    let safeBounds = SafeAreaManager.letsDoIt(view: self, upon: .bounds)

    /*let sDim = (Utils.isInPortraitState() ?  safeBounds.height + (self.workbenchView.frame.origin.y + self.workbenchView.frame.size.height) : safeBounds.width + (self.workbenchView.frame.size.width + self.workbenchView.frame.width))*/

    let sDim = CGFloat(0)

    var scDim = CGFloat(0)

    if Utils.isIPad() {
        if Utils.isInPortraitState() {
            scDim = safeBounds.height - (self.workbenchView.frame.origin.y  + 2 * (self.workbenchView.frame.size.height/3))
        } else {
            scDim = self.workbenchView.frame.origin.x  + self.workbenchView.frame.size.width/3
        }
    } else {
        if Utils.isInPortraitState() {
            scDim = safeBounds.height - self.workbenchView.frame.size.height
        } else {
            scDim = safeBounds.width - self.workbenchView.frame.size.width
        }
    }
    self.controlsView.setControlsType(controlsType: type, bounds: safeBounds, startingDim: sDim, scrolledDim: scDim)
}

1 个答案:

答案 0 :(得分:0)

似乎此代码不在您的视图控制器中,因此我看到了两个选项:

  1. 您可以将视图和滚动视图传递给上面共享的文件中的功能之一。然后,将约束应用于滚动视图,以便其框架与视图框架匹配。此功能将如下所示:

    func resizeScrollView(view: UIView, scrollView: UIScrollView) {
        NSLayoutConstraint(item: scrollView, attribute: .top, relatedBy: .equal, toItem: view, attribute: .top, multiplier: 1, constant: 0).isActive = true
        NSLayoutConstraint(item: scrollView, attribute: .trailing, relatedBy: .equal, toItem: view, attribute: .trailing, multiplier: 1, constant: 0).isActive = true
        NSLayoutConstraint(item: scrollView, attribute: .bottom, relatedBy: .equal, toItem: view, attribute: .bottom, multiplier: 1, constant: 0).isActive = true
        NSLayoutConstraint(item: scrollView, attribute: .leading, relatedBy: .equal, toItem: view, attribute: .leading, multiplier: 1, constant: 0).isActive = true
    }
    
  2. 或者您可以通过UIScreen访问设备屏幕的bounds属性,在这种情况下,该函数将如下所示:

    func resizeScrollView(scrollView: UIScrollView) {
        scrollView.bounds = UIScreen.main.bounds
    }
    

然后,您必须在视图控制器中设置滚动视图的位置。