Я пытаюсь реализовать кнопку изображения профиля в левом верхнем углу моего приложения.У меня проблемы с тем, чтобы заставить его выглядеть правильно.Кажется, что независимо от того, какие ограничения я накладываю на кнопку, она всегда получается гораздо крупнее и более однобокой, чем я хочу.Перед реализацией фактического контроллера навигации я просто использовал объект панели навигации и поместил кнопку поверх него.Однако я не могу сделать это с фактической панелью контроллера навигации.
Вот как это выглядело раньше: Как я хочу, чтобы оно выглядело
Вот как это выглядит после встраивания вида в контроллер навигации: Текущий
Вот как у меня есть кнопка, встроенная в панель навигации: Раскадровка
И, наконец, вот мой код:
class HomeViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
profileButton.layer.borderWidth = 1
profileButton.layer.masksToBounds = false
profileButton.layer.borderColor = Global.redColor.cgColor
profileButton.layer.cornerRadius = (profileButton.frame.height/2)
profileButton.clipsToBounds = true
profileButton.frame = CGRect(x: 0, y: 0, width: 30, height: 30)
setUpGestures()
}
func setUpGestures() {
let profileEdge = UIScreenEdgePanGestureRecognizer(target: self, action: #selector(profileEdgeSwiped))
profileEdge.edges = .left
view.addGestureRecognizer(profileEdge)
let settingsEdge = UIScreenEdgePanGestureRecognizer(target: self, action: #selector(settingsEdgeSwiped))
settingsEdge.edges = .right
view.addGestureRecognizer(settingsEdge)
}
@objc func profileEdgeSwiped(_ recognizer: UIScreenEdgePanGestureRecognizer) {
if recognizer.state == .recognized {
accountAction()
}
}
@objc func settingsEdgeSwiped(_ recognizer: UIScreenEdgePanGestureRecognizer) {
if recognizer.state == .recognized {
// Open settings menu (implement later)
}
}
@objc func profileAction() {
NotificationCenter.default.post(name: NSNotification.Name("ToggleSideMenu"), object: nil)
}
@IBOutlet weak var profileButton: UIButton!
@IBAction func profileAction(_ sender: Any) {
//NotificationCenter.default.post(name: NSNotification.Name("ToggleSideMenu"), object: nil)
accountAction()
}
func accountAction() {
let alert = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
alert.view.tintColor = Global.redColor
alert.addAction(UIAlertAction(title: "Log Out", style: .default, handler: { _ in
self.dismiss(animated: true, completion: nil)
}))
alert.addAction(UIAlertAction(title: "Add Account", style: .default, handler: { _ in
// Add additional account (implement later)
}))
alert.addAction(UIAlertAction.init(title: "Cancel", style: .cancel, handler: nil))
self.present(alert, animated: true, completion: nil)
} }