2017-09-29 89 views
0

我使用SwiftIcons創建一個按鈕,但該圖標的位置不正確。我添加一個負寬度來修復它:以編程方式創建的Swift UIBarButtonItem位置

let menuButton = UIBarButtonItem() 
menuButton.setIcon(icon: .ionicons(.chevronLeft), iconSize: 24, color: .white, cgRect: CGRect(x: 0, y: 0, width: 24, height: 24), target: self, action: #selector(menuButtonClick)) 
let negativeSpacer:UIBarButtonItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.fixedSpace, target: nil, action: nil) 
negativeSpacer.width = -13.7; 
self.navigationItem.leftBarButtonItems = [negativeSpacer, menuButton] 

這適用於iOS 10或更低版本,但在iOS 11上,寬度不帶負值。
Image
我該如何解決這個問題?

答:

let menuButton = UIBarButtonItem(title: "", style: .plain, target: self, action: #selector(menuButtonClick)) 
menuButton.image = UIImage.init(icon: .ionicons(.chevronLeft), size: CGSize(width: 24, height: 24)) 
menuButton.imageInsets = UIEdgeInsetsMake(0, -13.7, 0, 0) 
self.navigationItem.leftBarButtonItem = menuButton 

回答

0

也許你可以通過修改edgeInsets用這個方法您的按鈕嘗試:

UIEdgeInsets UIEdgeInsetsMake(CGFloat top, CGFloat left, CGFloat bottom, CGFloat right); 

它允許您添加您barButtonItem一些填充。 這裏是你如何與你的實際代碼中使用它:

negativeSpacer.imageInsets = UIEdgeInsetsMake(0, -13.7, 0, 0); 
+0

施加到negativeSpacer或菜單按鈕:( – rejjer

+0

正面和負面的價值觀沒有反應,但它的工作原理,當圖標添加爲barButton圖像,謝謝! – rejjer

相關問題