当前位置: 动力学知识库 > 问答 > 编程问答 >

ios - Swift - Not seeing my custom view class called from another custom view class

问题描述:

Just trying to follow some youtube tutorials and learn about custom views. I am sure I am missing something very basic here. After reading here and the net for hours I am giving up.

I have a custom view that slides out. I am planning on filling it with different custom views as needed. The slideOutView displays fine. However, I am not able to see the other custom view called FlashCardView inside it. I am calling FlashCardView from SlideOutView.

Thanks for the help!

code for FlashCardView class:

import UIKit

protocol FlashCardViewDelegate: class {

}

class FlashCardView: UIView {

weak var delegate: FlashCardViewDelegate?

let questionLabel = UILabel()

let scrollView = UIScrollView()

let dummyView = UIView()

//MARK: Initialization

override init(frame: CGRect) {

super.init(frame: frame)

self.backgroundColor = UIColor.black

}

required init?(coder aDecoder: NSCoder) {

fatalError("init(coder:) has not been implemented")

}

}

code for SlideOutView class:

import UIKit

class SlideOutView: UIView {

let slideOutView = UIView()

let menuBarLine = UIView()

let menuBarView = UIView()

let menuButton = UIButton()

let backButton = UIButton()

let flashCardView = FlashCardView()

let ratio = DeviceSpecificValues()

override init(frame: CGRect) {

super.init(frame: frame)

self.addView()

}

required init?(coder aDecoder: NSCoder) {

fatalError("init(coder:) has not been implemented")

}

func addView() {

if let window = UIApplication.shared.keyWindow {

slideOutView.frame = CGRect(x: 0, y: window.bounds.height, width: window.bounds.width, height: window.bounds.height / 2)

print("slideoutview frame is: \(slideOutView.frame)")

slideOutView.backgroundColor = UIColor.white

window.addSubview(slideOutView)

menuBarView.frame = CGRect(x: 0, y: 0, width: slideOutView.bounds.width, height: window.bounds.height / ratio.menuBarRatio)

menuBarView.backgroundColor = GlobalValues.menuBarViewColor

slideOutView.addSubview(menuBarView)

menuBarLine.frame = CGRect(x: 0, y: 0, width: menuBarView.bounds.width, height: GlobalValues.menuBarLineWidth)

menuBarLine.backgroundColor = GlobalValues.menuTextColor

menuBarView.addSubview(menuBarLine)

backButton.frame = CGRect(x: 0, y: 0, width: menuBarView.bounds.width * 0.1, height: menuBarView.bounds.height * 0.9)

backButton.center.y = menuBarView.bounds.height / 2 + GlobalValues.menuBarLineWidth

backButton.setTitle("<Back", for: .normal)

backButton.titleLabel?.adjustsFontSizeToFitWidth = true

backButton.setTitleColor(GlobalValues.menuTextColor, for: UIControlState())

backButton.showsTouchWhenHighlighted = true

menuBarView.addSubview(backButton)

menuButton.frame = CGRect(x: menuBarView.bounds.width * 0.9, y: 0, width: menuBarView.bounds.width * 0.1, height: menuBarView.bounds.height * 0.9)

menuButton.center.y = menuBarView.bounds.height / 2 + GlobalValues.menuBarLineWidth

menuButton.setTitle("Menu", for: .normal)

menuButton.titleLabel?.adjustsFontSizeToFitWidth = true

menuButton.setTitleColor(GlobalValues.menuTextColor, for: UIControlState())

menuButton.showsTouchWhenHighlighted = true

menuBarView.addSubview(menuButton)

addFlashCardView()

}

}

func slideIn() {

UIView.animate(withDuration: 0.5, delay: 0.0, options: .curveLinear, animations: {

if let window = UIApplication.shared.keyWindow {

self.slideOutView.frame = CGRect(x: 0, y: window.bounds.height, width: window.bounds.width, height: window.bounds.height / 2)

}

}, completion: nil)

}

func slideOut() {

UIView.animate(withDuration: 0.5, delay: 0.0, options: .curveLinear, animations: {

if let window = UIApplication.shared.keyWindow {

self.slideOutView.frame = CGRect(x: 0, y: window.bounds.height / 2, width: window.bounds.width, height: window.bounds.height / 2)

}

}, completion: nil)

}

func addFlashCardView() {

flashCardView.frame = slideOutView.frame

slideOutView.addSubview(flashCardView)

}

}

分享给朋友:
您可能感兴趣的文章:
随机阅读: