I'm trying to implement a profile image button in the top left of my application. I'm having trouble getting it to look correct. It seems that no matter what constraints I put on the button it always comes out much larger and more lopsided than what I want. Before implementing the actual navigation controller, I just used a navigation bar object and placed the button on top of it. However I can't do that with an actual navigation controller bar.
Here is what it looked like before: How I want it to Look
Here is what it looks like after embedding the view in a navigation controller: Current
Here is how I have the button embedded in the Navigation Bar: Storyboard
And finally here is my code:
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)
} }