侧面菜单帮助2

时间:2018-07-16 07:11:20

标签: swift xcode

enter image description here

我正在尝试与此一起使用tableviewcotroller,但是当查看按钮上的屏幕截图时,会看到一个我需要帮助修复的问题,因此,如果有人知道如何修复此plz帮助,我不想使用SWRevealViewController,我也处理所有代码而不是记分板感谢您的帮助

class SidebarView: UIView, UITableViewDelegate, UITableViewDataSource {

    var titleArr = [String]()

    weak var delegate: SidebarViewDelegate?

    override init(frame: CGRect) {
        super.init(frame: frame)
        self.clipsToBounds=true

        titleArr = ["Brittney Atwood", "Messages", "Contact", "Settings", "History", "Help", "Sign Out"]

        setupViews()

        myTableView.delegate=self
        myTableView.dataSource=self
        myTableView.register(UITableViewCell.self, forCellReuseIdentifier: "Cell")
        myTableView.tableFooterView=UIView()
        myTableView.separatorStyle = UITableViewCellSeparatorStyle.none
        myTableView.allowsSelection = true
        myTableView.bounces=false
        myTableView.showsVerticalScrollIndicator=false
        myTableView.backgroundView = UIImageView(image: #imageLiteral(resourceName: "Rectangle 96"))
        myTableView.isOpaque = false

    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return titleArr.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell=tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
        cell.backgroundColor = .clear
        cell.selectionStyle = .none
        if indexPath.row == 0 {
            let cellImg: UIImageView!
            cellImg = UIImageView(frame: CGRect(x: 15, y: 10, width: 80, height: 80))
            cellImg.layer.cornerRadius = 40
            cellImg.layer.masksToBounds=true
            cellImg.contentMode = .scaleAspectFill
            cellImg.layer.masksToBounds=true
            cellImg.image=#imageLiteral(resourceName: "user11")
            cell.addSubview(cellImg)

            let cellLbl = UILabel(frame: CGRect(x: 110, y: cell.frame.height/2-15, width: 250, height: 30))
            cell.addSubview(cellLbl)
            cellLbl.text = titleArr[indexPath.row]
            cellLbl.font=UIFont.systemFont(ofSize: 17)
            cellLbl.textColor=UIColor.white
        } else {
            cell.textLabel?.text=titleArr[indexPath.row]
            cell.textLabel?.textColor=UIColor.white
        }
        return cell
    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        self.delegate?.sidebarDidSelectRow(row: Row(row: indexPath.row))
    }

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        if indexPath.row == 0 {
            return 100
        } else {
            return 60
        }
    }

    func setupViews() {
        self.addSubview(myTableView)
        myTableView.topAnchor.constraint(equalTo: topAnchor).isActive=true
        myTableView.leftAnchor.constraint(equalTo: leftAnchor).isActive=true
        myTableView.rightAnchor.constraint(equalTo: rightAnchor).isActive=true
        myTableView.bottomAnchor.constraint(equalTo: bottomAnchor).isActive=true
    }

    let myTableView: UITableView = {
        let table=UITableView()
        table.translatesAutoresizingMaskIntoConstraints=false
        return table
    }()

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

2 个答案:

答案 0 :(得分:0)

请编写此代码,可能对您有帮助。

        var topSafeArea: CGFloat
        var bottomSafeArea: CGFloat

        if #available(iOS 11.0, *) {
            topSafeArea = view.safeAreaInsets.top
            bottomSafeArea = view.safeAreaInsets.bottom
        } else {
            topSafeArea = topLayoutGuide.length
            bottomSafeArea = bottomLayoutGuide.length
        }

        // safe area values are now available to use

谢谢。

答案 1 :(得分:0)

我不知道问题所在的视图控制器可能在我发布的两个文件之一中仍然无法解决

 class ViewController: UITableViewController {
 var sidebarView: SidebarView!
 var blackScreen: UIView!

override func viewDidLoad() {
    super.viewDidLoad()
    self.title = "Messages"

    let btnMenu = UIBarButtonItem(image: #imageLiteral(resourceName: "menu"), style: .plain, target: self, action: #selector(btnMenuAction))
    btnMenu.tintColor=UIColor(red: 54/255, green: 55/255, blue: 56/255, alpha: 1.0)
    self.navigationItem.leftBarButtonItem = btnMenu

    sidebarView=SidebarView(frame: CGRect(x: 0, y: 0, width: 0, height: self.view.frame.height))
    sidebarView.delegate=self
    sidebarView.layer.zPosition=100
    self.view.isUserInteractionEnabled=true
    self.navigationController?.view.addSubview(sidebarView)

    blackScreen=UIView(frame: self.view.bounds)
    blackScreen.backgroundColor=UIColor(white: 0, alpha: 0.5)
    blackScreen.isHidden=true
    self.navigationController?.view.addSubview(blackScreen)
    blackScreen.layer.zPosition=99
    let tapGestRecognizer = UITapGestureRecognizer(target: self, action: #selector(blackScreenTapAction(sender:)))
    blackScreen.addGestureRecognizer(tapGestRecognizer)
}

@objc func btnMenuAction() {
    blackScreen.isHidden=false
    UIView.animate(withDuration: 0.3, animations: {
        self.sidebarView.frame=CGRect(x: 0, y: 0, width: 250, height: self.sidebarView.frame.height)
    }) { (complete) in
        self.blackScreen.frame=CGRect(x: self.sidebarView.frame.width, y: 0, width: self.view.frame.width-self.sidebarView.frame.width, height: self.view.bounds.height+100)
    }
}

@objc func blackScreenTapAction(sender: UITapGestureRecognizer) {
    blackScreen.isHidden=true
    blackScreen.frame=self.view.bounds
    UIView.animate(withDuration: 0.3) {
        self.sidebarView.frame=CGRect(x: 0, y: 0, width: 0, height: self.sidebarView.frame.height)
    }
  }  
}

extension ViewController: SidebarViewDelegate {
func sidebarDidSelectRow(row: Row) {
    blackScreen.isHidden=true
    blackScreen.frame=self.view.bounds
    UIView.animate(withDuration: 0.3) {
        self.sidebarView.frame=CGRect(x: 0, y: 0, width: 0, height: self.sidebarView.frame.height)
    }
    switch row {
    case .editProfile:
        let vc=EditProfileVC()
        self.navigationController?.pushViewController(vc, animated: true)
    case .messages:
        print("Messages")
    case .contact:
        print("Contact")
    case .settings:
        print("Settings")
    case .history:
        print("History")
    case .help:
        print("Help")
    case .signOut:
        print("Sign out")
    case .none:
        break
        //        default:  //Default will never be executed
        //            break
    }
  }
}