ios - 滑入式菜单容器不滑动
问题描述
MainSideController 具有功能 toggleSideMenu。但是当我点击 ContentViewController 中的配置文件 ButtonTapped 时。该按钮显示它在调试菜单中被点击,但侧面菜单没有滑入。
import UIKit
class MainSideViewController: ContentViewController{
@IBOutlet weak var sideMenuContainer: UIView!
@IBOutlet weak var sideMenuViewLeadingConstraint: NSLayoutConstraint!
@IBOutlet weak var contentViewLeadingConstraint: NSLayoutConstraint!
var sideMenuVisible = false
override func viewDidLoad() {
super.viewDidLoad()
sideMenuViewLeadingConstraint.constant = 0 - self.sideMenuContainer.frame.size.width
}
@objc func toggleSideMenu(fromViewController: UIViewController) {
if(sideMenuVisible){
UIView.animate(withDuration: 0.5, animations: {
self.sideMenuViewLeadingConstraint.constant = 0 - self.sideMenuContainer.frame.size.width
self.contentViewLeadingConstraint.constant = 0
self.view.layoutIfNeeded()
})
} else {
self.view.layoutIfNeeded()
UIView.animate(withDuration: 0.5, animations: {
self.sideMenuViewLeadingConstraint.constant = 0
self.contentViewLeadingConstraint.constant = self.sideMenuContainer.frame.size.width
self.view.layoutIfNeeded()
})
}
sideMenuVisible = !sideMenuVisible
}
}
import UIKit
class ContentViewController: UIViewController {
let profileButton = UIButton(type: .custom)
override func viewDidLoad() {
super.viewDidLoad()
profileButton.setImage(UIImage(named: "hamburger") , for: .normal)
profileButton.contentMode = .scaleAspectFit
profileButton.translatesAutoresizingMaskIntoConstraints = false
// function performed when the button is tapped
profileButton.addTarget(self, action: #selector(profileButtonTapped(_:)), for: .touchUpInside)
// Add the profile button as the left bar button of the navigation bar
let barbutton = UIBarButtonItem(customView: profileButton)
self.navigationItem.leftBarButtonItem = barbutton
// Set the width and height for the profile button
NSLayoutConstraint.activate([
profileButton.widthAnchor.constraint(equalToConstant: 35.0),
profileButton.heightAnchor.constraint(equalToConstant: 35.0)
])
// Make the profile button become circular
profileButton.layer.cornerRadius = 35.0 / 2
profileButton.clipsToBounds = true
}
@IBAction func profileButtonTapped(_ sender: Any){
print("Button tapped")
if let mainVC = self.navigationController?.tabBarController? .parent as? MainSideViewController {
mainVC.toggleSideMenu(fromViewController: self)
}
}
}
我将 sideMenuViewLeadingConstraint 添加到 viewDidLoad。它仍然没有工作。我也添加了切换功能。它仍然没有滑入和滑出。
我希望配置文件按钮能够触发切换功能和侧边菜单,就像 twitter 侧边进出一样。
解决方案
在 MainSideViewController 中代替 sideMenuViewLeadingConstraint.constant = 0 - self.sideMenuContainer.frame.size.width 我使用 NotificationCenter.default.addObserver(self, selector: #selector(toggleSideMenu), name: NSNotification.Name("ToggleSideMenu"),对象:无)
并将其放在@IBAction func profileButtonTapped(_ sender: Any){ print("Button tapped")
NotificationCenter.default.post(name: NSNotification.Name("ToggleSideMenu"), object: nil) } }
推荐阅读
- python - 如何将嵌套字典中列表的总和添加回字典
- python - 为什么 sqlalchemy 似乎自发地提交某些查询而忽略其他查询?
- react-native - React Native 测试 android 和 ios 组件
- java - 关于覆盖类(数据类或 POJO)中的 toString() 方法和调用 list.toString() 的困惑?
- amazon-cognito - AWS Cognito 身份池创建的默认角色
- c# - '不能启用此约束,因为并非所有值都有对应的父值。'
- python - 正则表达式组不包括 az 但不包括 c
- nestjs - 我可以使用@nestjs/cofnig 从 appModule 中的数据库获取配置吗?
- laravel - Laravel Pluck 来自“与关系”
- javascript - 在 IndexedDB 中运行并行写入事务是否更快?