将UIView框架绑定到超级视图时,它会阻止UIscrollView滚动

时间:2018-08-28 12:23:30

标签: ios swift uiview uiscrollview

我目前有一个UIView,用于表示图像要去的地方,我使用了约束和锚点将UIView的框架绑定到超级视图,从而覆盖了整个屏幕,但是一旦我这样做,它便停止了UIScrollView能够滚动到下面的是UIScrollView和我的ViewController的代码

profileView.swift

import Foundation
import UIKit
import PureLayout

extension UIView {

func bindFrameToSuperviewBounds() {
    guard let superview = self.superview else {
        print("Error! `superview` was nil – call `addSubview(view: UIView)` before calling `bindFrameToSuperviewBounds()` to fix this.")
        return
    }

    self.translatesAutoresizingMaskIntoConstraints = false
    self.leadingAnchor.constraint(equalTo: superview.leadingAnchor).isActive = true
    self.trailingAnchor.constraint(equalTo: superview.trailingAnchor).isActive = true

      }
}

class profileView: UIScrollView {
var shouldSetupConstraints = true

var profileImageContainer = UIView()
var profileImage = UIImageView(image: UIImage(named: "logo"))
var profileInfo = UIView()
var name = UILabel()

let screenSize = UIScreen.main.bounds
let gradient = CAGradientLayer()
let profilegradient = CAGradientLayer()
let textFieldAppearance = UITextField.appearance()

override init(frame: CGRect) {
    super.init(frame: frame)

    gradient.colors = [UIColor(red: (96/255.0), green: (95/255.0), blue: (95/255.0), alpha: 0.65).cgColor , UIColor(red: (35/255.0), green: (39/255.0), blue: (42/255.0), alpha: 1).cgColor]
    gradient.startPoint = CGPoint(x: 0, y: 0)
    gradient.endPoint = CGPoint(x: 0, y: 1)
    gradient.cornerRadius = 4

    profilegradient.colors = [UIColor(red: (96/255.0), green: (95/255.0), blue: (95/255.0), alpha: 0.65).cgColor , UIColor(red: (35/255.0), green: (39/255.0), blue: (42/255.0), alpha: 1).cgColor]
    profilegradient.startPoint = CGPoint(x: 0, y: 0)
    profilegradient.endPoint = CGPoint(x: 0, y: 1)

    profileImageContainer.frame = CGRect(x: 0, y:0, width: screenSize.width, height: screenSize.height)
    profilegradient.frame = profileImageContainer.bounds
    profileImageContainer.layer.insertSublayer(profilegradient,at:0)
    self.addSubview(profileImageContainer)
    profileImageContainer.bindFrameToSuperviewBounds()
    profileImage.center = CGPoint(x: screenSize.width/2, y:screenSize.height/2)
    profileImageContainer.addSubview(profileImage)
    profileInfo.frame = CGRect(x: 0, y: screenSize.height/1.3, width: screenSize.width, height: 150)
   gradient.frame = profileInfo.bounds
   profileInfo.layer.insertSublayer(gradient,at:0)
   self.addSubview(profileInfo)

   name.frame = CGRect(x: screenSize.width/20, y: 0, width: screenSize.width, height: 50)
   name.font = UIFont(name: "HelveticaNeue-medium", size: 32)
   name.textColor = .white
   profileInfo.addSubview(name)
   textFieldAppearance.keyboardAppearance = .light

}

required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
}


override func updateConstraints() {
    if(shouldSetupConstraints) {
        // AutoLayout constraints
        shouldSetupConstraints = false
    }
    super.updateConstraints()
   } 
}

viewcontoller.swift

class profile: UIViewController{

let defaults = UserDefaults.standard
var profileViewer: profileView!
let screenSize = UIScreen.main.bounds

override func viewDidLoad() {
    super.viewDidLoad()
    self.load()
    self.view.backgroundColor = UIColor(red: (255/255.0), green: (255/255.0), blue: (255/255.0), alpha: 1)
    profileViewer = profileView(frame: CGRect(x: 0, y: 0, width: screenSize.width, height: screenSize.height))
    profileViewer.contentSize = CGSize(width: screenSize.width, height: 1000)
    profileViewer.isUserInteractionEnabled = true
    self.view.addSubview(profileViewer)
  }
}

0 个答案:

没有答案