swift - 侧边菜单帮助 2
问题描述
我正在尝试使用 tableviewcotroller 但是当查看按钮上的屏幕截图时会看到我需要帮助修复的问题所以如果有人知道如何修复这个请帮助我不想使用 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")
}
}
解决方案
请编写此代码,它可能对您有所帮助。
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
谢谢你。
推荐阅读
- python - 错误的请求套接字python
- reporting-services - 报表/Power BI 服务器电子邮件订阅:将原始 Web 链接更改为别名
- regex - 如何在编译器设计中处理正则表达式中的“except”?
- mysql - MySql Docker Container 的数据在 docker-compose up 后不持久
- javascript - 单击 div 内的输入时调用 angular div (clickOutside)
- swift - 带有可选选项的致命错误没有意义
- javascript - 如何检查应用程序是否在 Reactjs App 的另一个选项卡中打开
- android - Android - 如何创建 CheckBox 可绘制对象?
- vue.js - 同一页面的参数 id 更改上的 Vue 刷新页面
- postgresql - 如何在 Slick 中将案例类映射为 postgresql 列