我将自定义按钮添加到导航控制器
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"back.png"] style:UIBarButtonItemStylePlain target:self action:@selector(backAction)]; self.navigationItem.leftBarButtonItem = backButton;
它工作正常,但按钮显示有界限。我该如何解决这个问题?
更新 我找到了解决方案
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;
答案 0 :(得分:14)
试试这个。
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];
答案 1 :(得分:1)
对于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)
动作(选择器)应如下所示:
@objc func backAction () {
// do the magic
}
答案 2 :(得分:0)
根据要求在左侧或右侧添加带标题或不带标题的按钮,
google: {family: ['Roboto Mono: 500: Latin, Cyrillic']},
右键操作
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
}
}
}
}
左键操作
@objc func btnRightNavigationClicked(sender: UIButton) {
// add your logic here
}