我正在使用y位置低于默认值的项目自定义导航栏。我尝试了很多方法:
setBackButtonBackgroundVerticalPositionAdjustment:
CGRectOffset
UIBarButtonItem.appearance().setTitlePositionAdjustment(UIOffset.init(horizontal: X, vertical: Y), forBarMetrics: UIBarMetrics.Default)
他们让物品出现在正确的位置,但是当我点击/触摸它们时,我无法触发任何动作。我认为因为我没有碰到酒吧物品,所以它们只是这些物品的外观/复制视图/图像。
有人可以帮我解决这个问题吗?
我的代码:
let lblTitle = UILabel(frame: CGRectMake(0, 0, 100, 28))
lblTitle.text = FMLStringLocale("APP_name")
lblTitle.textColor = UIColor.whiteColor()
lblTitle.font = ROBOTO_THIN_24
lblTitle.textAlignment = NSTextAlignment.Left
lblTitle.backgroundColor = UIColor.clearColor()
lblTitle.transform = CGAffineTransformMakeTranslation(6, 11)
let lblTitleContainer = UIView(frame: lblTitle.frame)
lblTitleContainer.addSubview(lblTitle)
let leftTitle = UIBarButtonItem(customView: lblTitleContainer)
navigationItem.leftBarButtonItem = leftTitle
let rightView = UIView(frame: CGRectMake(0, 0, 134, 22))
rightView.backgroundColor = UIColor.whiteColor()
rightView.transform = CGAffineTransformMakeTranslation(0, 18)
let imvUSFlag = UIImageView(image: UIImage(named: "icon_us"))
imvUSFlag.frame = CGRectMake(0, 0, 19, 13)
imvUSFlag.transform = CGAffineTransformMakeTranslation(-2, 20)
rightView.addSubview(imvUSFlag)
let switcher = UISwitch(frame: CGRectMake(imvUSFlag.frame.maxX - 5, 12, 31, 16))
switcher.setOn(false, animated: false)
switcher.addTarget(self, action: Selector("tapToChangeLanguage:"), forControlEvents: UIControlEvents.ValueChanged)
switcher.transform = CGAffineTransformMakeScale(0.5, 0.5)
// let switchContainer = UIView(frame: switcher.frame)
// switchContainer.addSubview(switcher)
rightView.addSubview(switcher)
let imvFRFlag = UIImageView(image: UIImage(named: "icon_france"))
imvFRFlag.frame = CGRectMake(switcher.frame.maxX + 6, 0, 19, 14)
imvFRFlag.transform = CGAffineTransformMakeTranslation(0, 20)
rightView.addSubview(imvFRFlag)
let btnMenu = UIButton()
btnMenu.setImage(UIImage(named : "icon_menu"), forState: .Normal)
btnMenu.frame = CGRectMake(imvFRFlag.frame.maxX + 30, 0, 23, 21)
btnMenu.addTarget(self, action: "tapToMenu:", forControlEvents: UIControlEvents.TouchUpInside)
btnMenu.transform = CGAffineTransformMakeTranslation(0, 18)
rightView.addSubview(btnMenu)
rightView.transform = CGAffineTransformMakeTranslation(0, 18)
let rightViewItem = UIBarButtonItem(customView: rightView)
navigationItem.rightBarButtonItem = rightViewItem
navigationController?.navigationBar.barStyle = .BlackTranslucent
navigationController?.navigationBar.setBackgroundImage(UIImage(), forBarMetrics: .Default)
navigationController?.navigationBar.shadowImage = UIImage()
答案 0 :(得分:2)
您无法点按按钮btnMenu
的原因是您已将btnMenu
置于其超级视图rightView
的框架外。超级视图外的子视图无法接收。
答案 1 :(得分:1)
最后,我解决了我的问题:
检查我的新代码:
let lblTitle = UILabel(frame: CGRectMake(7, 32, 100, 28))
lblTitle.text = FMLStringLocale("APP_name")
lblTitle.textColor = UIColor.whiteColor()
lblTitle.font = ROBOTO_THIN_24
lblTitle.textAlignment = NSTextAlignment.Left
lblTitle.backgroundColor = UIColor.clearColor()
let lblTitleContainer = UIView(frame: CGRectMake(0, 0, 100, 64))
// lblTitleContainer.bounds = CGRectOffset(lblTitleContainer.bounds, -6, -32)
lblTitleContainer.addSubview(lblTitle)
let leftTitle = UIBarButtonItem(customView: lblTitleContainer)
navigationItem.leftBarButtonItem = leftTitle
let rightView = UIView(frame: CGRectMake(0, 0, 134, 64))
// rightView.backgroundColor = UIColor.grayColor()
// rightView.transform = CGAffineTransformMakeTranslation(0, 18)
let imvUSFlag = UIImageView(image: UIImage(named: "icon_us"))
imvUSFlag.frame = CGRectMake(0, 40, 18, 12)
rightView.addSubview(imvUSFlag)
let switcher = UISwitch(frame: CGRectMake(imvUSFlag.frame.maxX - 5, 32, 31, 16))
switcher.setOn(false, animated: false)
switcher.addTarget(self, action: Selector("tapToChangeLanguage:"), forControlEvents: UIControlEvents.ValueChanged)
switcher.transform = CGAffineTransformMakeScale(0.5, 0.45)
let switchContainer = UIView(frame: switcher.frame)
switchContainer.addSubview(switcher)
rightView.addSubview(switcher)
let imvFRFlag = UIImageView(image: UIImage(named: "icon_france"))
imvFRFlag.frame = CGRectMake(switcher.frame.maxX + 6, 40, 18, 12)
rightView.addSubview(imvFRFlag)
let btnMenu = UIButton()
btnMenu.setImage(UIImage(named : "icon_menu"), forState: .Normal)
btnMenu.frame = CGRectMake(imvFRFlag.frame.maxX + 30, 35, 23, 21)
btnMenu.addTarget(self, action: "tapToMenu:", forControlEvents: UIControlEvents.TouchUpInside)
rightView.addSubview(btnMenu)
let rightViewItem = UIBarButtonItem(customView: rightView)
navigationItem.rightBarButtonItem = rightViewItem
navigationController?.navigationBar.barStyle = .BlackTranslucent
navigationController?.navigationBar.setBackgroundImage(UIImage(), forBarMetrics: .Default)
navigationController?.navigationBar.shadowImage = UIImage()