ios 将自定义按钮添加到无边框导航控制器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5621160/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Add custom button to navigation controller without border
提问by Tunyk Pavel
I add custom button to navigation controller
我将自定义按钮添加到导航控制器
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"back.png"] style:UIBarButtonItemStylePlain target:self action:@selector(backAction)]; self.navigationItem.leftBarButtonItem = backButton;
it works fine, but button appears bordered. How can I fix that?
它工作正常,但按钮出现边框。我该如何解决?
UPDATEI found solution
更新我找到了解决方案
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 25, 25)]; [button setImage:[UIImage imageNamed:@"back.png"] forState:UIControlStateNormal]; [button addTarget:self action:@selector(buttonFavoriteClicked) forControlEvents:UIControlEventTouchUpInside]; UIBarButtonItem *back = [[UIBarButtonItem alloc] initWithCustomView:button]; [button release];
self.navigationItem.leftBarButtonItem = back;
回答by EmptyStack
Try this.
尝试这个。
UIButton *backButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, button_width, button_height)];
[backButton setImage:[UIImage imageNamed:@"back.png"] forState:UIControlStateNormal];
[backButton addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
回答by Alsh compiler
For Swift 4
对于 Swift 4
let backButton = UIButton(frame: CGRect(x: 0, y: 0, width: 25, height: 25))
backButton.setImage(UIImage(named: "back.png"), for: .normal)
backButton.addTarget(self, action: #selector(backAction), for: .touchUpInside)
self.navigationItem.rightBarButtonItem = UIBarButtonItem(customView: backButton)
And action (selector) should be like the following :
动作(选择器)应该如下所示:
@objc func backAction () {
// do the magic
}
回答by Pooja Gupta
Adding Button either in left or right with title or without title as per the requirement,
根据要求在左侧或右侧添加带标题或不带标题的按钮,
func addBarButton(image: UIImage?, isLeft: Bool = true, title: String? = nil, font: UIFont = UIFont.systemFont(ofSize: 16), tintColor: UIColor = R.color.textfieldColor() ?? .black) {
if let navController = self.navigationController {
let navBar = navController.navigationBar
navBar.isHidden = false
navBar.isTranslucent = false
let btn = UIButton.init(type: .custom)
if let img = image {
btn.setImage(img, for: UIControl.State.normal)
}
if let titleTxt = title {
btn.setTitle(titleTxt, for: .normal)
btn.setTitleColor(tintColor, for: .normal)
btn.titleLabel?.font = font
}
btn.frame = CGRect.init(x: 0, y: 0, width: 35, height: 35)
btn.adjustsImageWhenHighlighted = false
let barButton = UIBarButtonItem.init(customView: btn)
if isLeft {
btn.addTarget(self, action: #selector(self.btnLeftNavigationClicked), for: UIControl.Event.touchUpInside)
btn.tintColor = tintColor
if let tabBar = self.tabBarController {
tabBar.navigationItem.leftBarButtonItem = nil
tabBar.navigationItem.leftBarButtonItem = barButton
} else {
self.navigationItem.leftBarButtonItem = nil
self.navigationItem.leftBarButtonItem = barButton
}
} else {
btn.addTarget(self, action: #selector(self.btnRightNavigationClicked), for: UIControl.Event.touchUpInside)
btn.tintColor = tintColor
if let tabBar = self.tabBarController {
tabBar.navigationItem.rightBarButtonItem = barButton
} else {
self.navigationItem.rightBarButtonItem = barButton
}
}
}
}
Right button action
右键操作
@objc func btnRightNavigationClicked(sender: UIButton) {
// add your logic here
}
Left button action
左键动作
@objc func btnLeftNavigationClicked(sender: UIButton) {
guard let navigationController = self.navigationController else {
return
}
navigationController.popViewController(animated: true)
}