它工作正常,直到最近的更新,我认为导航项应该与AutoLayout概念一起使用。我一直在用它:
let button = UIButton(frame: CGRect(origin: CGPoint.zero, size: CGSize(width: 30, height: 30)))
button.setImage(UIImage(named: "BackIcon"), for: UIControlState())
button.imageEdgeInsets = UIEdgeInsetsMake(0, -20, 0, 0)
button.addTarget((target != nil ? target! : self), action: backAction, for: .touchUpInside)
navigationItem.leftBarButtonItem = UIBarButtonItem(customView: button)
我应该做些什么改变才能使它平滑,因为它不会在每次敲击时被调用,通常需要2-3次才能在同一区域内进行调整。
答案 0 :(得分:0)
这适用于iOS 11.检查并查看它是否适合您。
let btnLeftMenu = UIButton.init(type: .system)
let image = UIImage(named: "Back");
btnLeftMenu.setImage(image, for: .normal)
btnLeftMenu.setTitle("BACK", for: .normal);
btnLeftMenu.imageEdgeInsets = UIEdgeInsetsMake(0, -30, 0, 0)
btnLeftMenu.titleEdgeInsets = UIEdgeInsetsMake(0, -30, 0, 0)
btnLeftMenu.sizeToFit()
btnLeftMenu.tintColor = UIColor.white
btnLeftMenu.titleLabel?.font = UIFont.init(name: "Avenir-Heavy", size: 16.0)
btnLeftMenu.addTarget(self, action: #selector (CustomMessageVC.backButtonAction(_:)), for: .touchUpInside)
let barButton = UIBarButtonItem(customView: btnLeftMenu)
self.navigationItem.leftBarButtonItem = barButton
[编辑]
图像的宽度和高度可能很小。您可以使用Xcode调试上的实时Viewstack检查后退按钮的宽度和高度。单击图像右侧的第二个按钮以查看ViewStack。如果宽度和高度是问题,则增加按钮宽度和高度,图像居中,imageView属性适合纵横比。这应该可以解决你的问题。快乐的编码。
答案 1 :(得分:0)
适合我:
let backButton = UIBarButtonItem(title: "Back",
style: .done,
target: self,
action: #selector(moveBack))
navigationItem.setLeftBarButton(backButton, animated: false)
打开视图调试器并检查leftBarButtonItem的框架。您应该对leftBarButton
小心,因为它不会从UIView
继承并在运行时生成帧。
答案 2 :(得分:0)
在我们的应用程序中发现了这个问题,因为iOS 11已经出局了,所以这是我的解决方案。它并不完美,因为你需要设置按钮的框架,但是完成工作,并且可以在iOS 9.3 / 10.3.1 / 11.1上工作(可能在两者之间)。 Tap区域正常,UX很好。
我在viewDidLoad
中调用它:
func setNavbarButtons() {
// setup the left and right nav bar buttons
// manually define the frame for the buttons
let buttonWidth: CGFloat = 44
var buttonHeight: CGFloat = buttonWidth
// if possible, use the nav bar height as the button height, else fall back to the manual value above
if let frame = self.navigationController?.navigationBar.frame {
buttonHeight = frame.height
}
// apply this to the left inset for the left button, right inset for the right button, so the image is pushed to the left/right respectively
// (hence the negative value)
// setting this to 0 will center the image in the button and we don't want that
let edgeInset = -(buttonWidth/2)
// setup a button to hold the image (left)
let leftButton = UIButton(type: .custom)
let backIcon = UIImage(named: "Back with shadow")
backIcon!.isAccessibilityElement = true
backIcon!.accessibilityLabel = "Back"
// no title text
leftButton.setTitle("", for: .normal)
leftButton.setImage(backIcon, for: .normal)
leftButton.sizeToFit()
leftButton.addTarget(self, action: #selector(self.didTapLeftNavbarButton), for: .touchUpInside)
// define left button frame and inset
leftButton.frame.size.width = buttonWidth
leftButton.frame.size.height = buttonHeight
leftButton.contentEdgeInsets = UIEdgeInsetsMake(0, edgeInset, 0, 0)
// finally setup a UIBarButtonItem to hold the UIButton (arg)
let leftBarButtonItem = UIBarButtonItem(customView: leftButton)
leftBarButtonItem.title = ""
// set it
self.navigationItem.setLeftBarButton(leftBarButtonItem, animated: true)
// rinse/wash/repeat for right button
let rightButton = UIButton(type: .custom)
let shareIcon = UIImage(named: "Share")
shareIcon!.isAccessibilityElement = true
shareIcon!.accessibilityLabel = "Share"
// no title text
rightButton.setTitle("", for: .normal)
rightButton.setImage(shareIcon, for: .normal)
rightButton.sizeToFit()
rightButton.addTarget(self, action: #selector(self.didTapActionButton(_:)), for: .touchUpInside)
// define right button frame and inset
rightButton.frame.size.width = buttonWidth
rightButton.frame.size.height = buttonHeight
rightButton.contentEdgeInsets = UIEdgeInsetsMake(0, 0, 0, edgeInset)
let rightBarButtonItem = UIBarButtonItem(customView: rightButton)
rightBarButtonItem.title = ""
self.navigationItem.setRightBarButton(rightBarButtonItem, animated: true)
}
答案 3 :(得分:0)
在我的自定义public IList<Models.MyModel> GetDaraByPId(Guid PId)
{
var query = this._Repository.GetIQueryable();
var list = query.Select(a => new Models.MyModel
{
MyModelId = a.Id,
MyModelName = a.Name,
PId=a.PId
}).Where(f => f.PId == PId);
return list.ToList();
}
课程中覆盖以下方法对我来说很有用:
UIView