在swift中创建幻灯片菜单时,我有一个奇怪的问题,
我使用AKSwiftSlideMenu作为参考来构建菜单,
这只发生在具有UITableViewDataSource,UITableViewDelegate的ViewControllers上。
如果我在没有一个视频控制器的情况下转到其他视图控制器,则菜单显示正常。
下面是我的BaseViewController代码
protocol SlideMenuDelegate {
func slideMenuItemSelectedAtIndex(_ index : Int32)
}
class MenuViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
/**
* Array to display menu options
*/
@IBOutlet var tblMenuOptions : UITableView!
/**
* Transparent button to hide menu
*/
@IBOutlet var btnCloseMenuOverlay : UIButton!
/**
* Array containing menu options
*/
var arrayMenuOptions = [Dictionary<String,String>]()
/**
* Menu button which was tapped to display the menu
*/
var btnMenu : UIButton!
/**
* Delegate of the MenuVC
*/
var delegate : SlideMenuDelegate?
override func viewDidLoad() {
super.viewDidLoad()
tblMenuOptions.tableFooterView = UIView()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
updateArrayMenuOptions()
}
func updateArrayMenuOptions(){
arrayMenuOptions.append(["title":"Locations", "icon":"LocationIcon"])
arrayMenuOptions.append(["title":"Offers", "icon":"OffersIcon"])
arrayMenuOptions.append(["title":"Feedback", "icon":"FeedbackIcon"])
arrayMenuOptions.append(["title":"About", "icon":"AboutIcon"])
arrayMenuOptions.append(["title":"Logout", "icon":"LogoutIcon"])
tblMenuOptions.reloadData()
}
@IBAction func onCloseMenuClick(_ button:UIButton!){
btnMenu.tag = 0
if (self.delegate != nil) {
var index = Int32(button.tag)
if(button == self.btnCloseMenuOverlay){
index = -1
}
delegate?.slideMenuItemSelectedAtIndex(index)
}
UIView.animate(withDuration: 0.3, animations: { () -> Void in
self.view.frame = CGRect(x: -UIScreen.main.bounds.size.width, y: 0, width: UIScreen.main.bounds.size.width,height: UIScreen.main.bounds.size.height)
self.view.layoutIfNeeded()
self.view.backgroundColor = UIColor.clear
}, completion: { (finished) -> Void in
self.view.removeFromSuperview()
self.removeFromParentViewController()
})
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell : UITableViewCell = tableView.dequeueReusableCell(withIdentifier: "cellMenu")!
cell.selectionStyle = UITableViewCellSelectionStyle.none
cell.layoutMargins = UIEdgeInsets.zero
cell.preservesSuperviewLayoutMargins = false
cell.backgroundColor = UIColor.clear
let lblTitle : UILabel = cell.contentView.viewWithTag(101) as! UILabel
let imgIcon : UIImageView = cell.contentView.viewWithTag(100) as! UIImageView
imgIcon.image = UIImage(named: arrayMenuOptions[indexPath.row]["icon"]!)
lblTitle.text = arrayMenuOptions[indexPath.row]["title"]!
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let btn = UIButton(type: UIButtonType.custom)
btn.tag = indexPath.row
self.onCloseMenuClick(btn)
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return arrayMenuOptions.count
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1;
}
}
MenuViewController
class LocationViewController: BaseViewController, CLLocationManagerDelegate, UITableViewDataSource, UITableViewDelegate {
var locItems:Array<LocItems>?
var locItemsWrapper:LocItemsWrapper?
var isLoadingLocItems = false
private var tb: UITableView?
override func viewDidLoad() {
super.viewDidLoad()
//set up tableview
tb = UITableView()
//Set button to open up menu
self.addSlideMenuButton()
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if (self.locItems == nil) {
return 0
}
return self.locItems!.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "locCell", for: indexPath) as! LocItemCell
if(self.locItems != nil && self.locItems!.count >= indexPath.row) {
let locItem = self.locItems![indexPath.row]
let rowsLoaded = self.locItems!.count
if (!self.isLoadingLocItems && (indexPath.row >= (rowsLoaded - rowsToLoadFromBottom))) {
let totalRows = self.locItemsWrapper!.count!
let remainingLocItemsToLoad = totalRows - rowsLoaded
if(remainingLocItemsToLoad > 0) {
self.loadMoreLocItems()
}
}
}
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if(self.locItems!.count >= indexPath.row) {
selectedLocId = self.locItems![indexPath.row].id!
selectedLocBg = self.locItems![indexPath.row].locationBackground!
//TODO: Set up Switch on api call
let parameters = [
"locId": selectedLocId
]
}
}
override func prepare(for segue: (UIStoryboardSegue!), sender: Any!) {
if(segue.identifier == "showCongratOffer") {
let svc = segue.destination as! CongratOfferViewController
svc.offerId = selectedLocId
svc.type = 1
}
}
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
if((indexPath.row % 2) == 0) {
cell.backgroundColor = UIColor(red: 0.9, green: 0.9, blue: 0.9, alpha: 1.0)
} else {
cell.backgroundColor = UIColor.white
}
}
发生这种情况的视图控制器示例
print(self.childViewControllers[0].topLayoutGuide)
Empty Space Issue -> <_UILayoutGuide: 0x7fce6e7115e0; frame = (0 0; 0 0); hidden = YES; layer = <CALayer: 0x600000238700>>
Working -> <_UILayoutGuide: 0x7fce6e51ad30; frame = (0 0; 0 64); hidden = YES; layer = <CALayer: 0x60000023c9a0>>
我知道它有很多代码,我试图在示例中删除尽可能多的部分,同时保持菜单代码完整,
如果有人能帮助我解决这个问题或者指出我正确的方向,我真的很感激。
打开菜单之前的tableview图片,
故事板的图片, 左上角的控制器可以忽略, 从登录(中间的顶部),我们前往菜单所在的位置(右下角,中下) about(右上角)将正确显示菜单,没有空格, 菜单控制器在左下角。
菜单仍然有效,尽管空白区域显示在带有UITableViewDataSource,UITableViewDelegate的控制器中。
更新:查看视图及其构建方式,我发现菜单高度限制是不同的。它没有在BaseViewController中的menuVC.view.layoutIfNeeded()之后设置。
def pascal(n):
row = [1]
y = [0]
for x in range(n):
print(row)
row = [l+r for l, r in zip(row+y, y+row)]
return n
pascal(4)
您如何仅更改视图上的高度约束? 我可以得到像这样的只读视图约束, self.childViewControllers [0] .view.constraints
答案 0 :(得分:1)
我现在看到了这个问题。您需要做的是从TableView复制单元格,删除原始单元格,确保TableView完全为空,然后重新粘贴Cell。您也可以通过完全创建一个新单元格来手动执行此操作。顺便说一句,问题是你的单元格上方有一个空的空间需要从TableView中删除。