简体   繁体   English

如何在导航栏中的按钮上设置约束? iOS 13 Swift 5(将圆形轮廓图像按钮添加到导航控制器)

[英]How to set constraints on a button in a navigation bar? iOS 13 Swift 5 (Add circular profile image button to navigation controller)

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.在实现实际导航 controller 之前,我只是使用了一个导航栏 object 并将按钮放在它上面。 However I can't do that with an actual navigation controller bar.但是我不能用实际的导航 controller 栏做到这一点。

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这是在导航 controller 中嵌入视图后的样子:当前

Here is how I have the button embedded in the Navigation Bar: Storyboard这是我在导航栏中嵌入按钮的方式: 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)
}    }

Can you check one time commenting as of now i am not having a mac right now你能检查一次评论吗?我现在没有mac

also check this post as you may have width issue Change width of a UIBarButtonItem in a UINavigationBar还要检查这篇文章,因为您可能有宽度问题在 UINavigationBar 中更改 UIBarButtonItem 的宽度

//comment this code //注释这段代码

profileButton.clipsToBounds = true profileButton.clipsToBounds = true

i hope it helps as one time am having same issue.我希望它会有所帮助,因为有一次我遇到了同样的问题。

also can you check it with using init(customView: UIView) hope it helps你也可以使用init(customView: UIView)检查它希望它有帮助

Here is my temporary solution: First, scale the default profile image in photoshop to 90 x 90 px or use this website: Resize Image .这是我的临时解决方案:首先,将 Photoshop 中的默认个人资料图像缩放为 90 x 90 像素或使用此网站:调整图像大小 Delete any previous buttons and drag a new button into the navigation bar from the storyboard.删除任何以前的按钮并将新按钮从 storyboard 拖到导航栏中。 Set the button name to "" and the image to whichever asset is your resized image.将按钮名称设置为“”,并将图像设置为您调整大小的图像的任何资产。 In viewDidLoad() set the boarder radius to 15. What I do when I need to change the image is call this extension and resize the width to 30. The reason it's temporary is because if you change anything in the story board, the image stops rendering properly and you have to repeat the steps above to get it working again.在 viewDidLoad() 中将边界半径设置为 15。当我需要更改图像时,我所做的是调用此扩展并将宽度调整为 30。它是临时的原因是因为如果您更改故事板中的任何内容,图像就会停止正确渲染,您必须重复上述步骤才能使其再次工作。

extension UIImage {
func resizeImage (newWidth: CGFloat) -> UIImage {
    let scale = newWidth / self.size.width
    let newHeight = self.size.height * scale
    let newSize = CGSize(width: newWidth, height: newHeight)

    let renderer = UIGraphicsImageRenderer(size: newSize)

    let image = renderer.image { (context) in
        self.draw(in: CGRect(origin: CGPoint(x: 0, y: 0), size: newSize))
    }
    return image
} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在导航栏上添加按钮iOS Swift - how to add button on navigation bar IOS swift Swift / XCode 6.4:向导航栏中的导航控制器添加后退按钮 - Swift/XCode 6.4: add back button to navigation controller in navigation bar iOS 13 中第二个视图的导航栏按钮 alignment 错误 controller - Wrong alignment of navigation bar button for second view controller in iOS 13 ios swift 如何在导航栏中设置保存按钮 - ios swift how to set a save button in the navigation bar 使用 Tab Bar Controller Swift 在导航栏中添加新的栏按钮 - Add new bar button in Navigation Bar with a Tab Bar Controller Swift 如何在swift 4中的导航栏上添加图像后退按钮 - how to add image back button on navigation bar in swift 4 如何在没有导航的情况下在导航栏中添加栏按钮 controller。 - How to add Bar Button in navigation bar without navigation controller. 如何在导航栏中添加图像和按钮设计? - How to add image and button design to the navigation bar? 如何更改 iOS 13 导航栏中的后退按钮图像? - How can I change the back button image in the navigation bar for iOS 13? 自定义iOS导航栏高度,添加背景,菜单按钮(Swift) - Customizing iOS Navigation Bar height, add background, menu button (Swift)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM