我想在我的导航栏中再添一个图标,但无法弄清楚如何调整宽度以获得更多空间。
使用标准的BarButtonItems并使用故事板构建,iPad上的纵向模式似乎有14个图标限制。这是一款仅限iPad的应用程序。
这是我添加第15个图标时得到的结果:
您可以看到"页面"图标位于" +"图标。
我在界面构建器中找不到允许调整BarButton大小的设置。我似乎无法找到程序化的解决方案。但是,我可以找到许多具有16个或更多图标的应用程序示例。
我还没有尝试将导航栏更改为工具栏(并且无论如何都没有真正看到明显的解决方案)。
任何人都知道如何挤出更多空间,以便我可以再插入一个图标?
答案 0 :(得分:0)
使用UIBarButtonItems
创建initWithCustomView:
。通过这种方式,您可以完全控制项目框架,并可以将其缩小。
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"button-image.png"]];
imageView.frame = CGRectMake(0, 0, 43, 30);
UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:imageView];
self.navigationItem.leftBarButtonItem = barButtonItem;
答案 1 :(得分:0)
与我的实施熊有一点点问题。该事件必须由用于创建自定义视图的UIImageView处理。我发现在stackoverflow的另一个问题中更容易实现。它涉及使customView成为自定义UIButton。请参阅:UIBarButtonItem Custom view in UINavigationBar
结果:
这是我在Swift中几乎完整(但未优化)的实现。
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let navigationBar = UINavigationBar(frame: CGRectMake(0, 20, self.view.frame.size.width, 44)) // Offset by 20 pixels vertically to take the status bar into account
navigationBar.backgroundColor = UIColor.whiteColor()
//create a blank image for padding
var rect = CGRectMake(0, 0, 30, 30)
var size = CGSizeMake(30, 30)
UIGraphicsBeginImageContextWithOptions(size, false, 0)
UIColor.clearColor().setFill()
UIRectFill(rect)
var image: UIImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
let notButtonBig = UIBarButtonItem(customView: UIImageView(image: image))
rect = CGRectMake(0, 0, 4, 30)
size = CGSizeMake(4, 30)
UIGraphicsBeginImageContextWithOptions(size, false, 0)
UIColor.clearColor().setFill()
UIRectFill(rect)
image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
let notButtonSmall = UIBarButtonItem(customView: UIImageView(image: image))
//clear - a regular old BarButton
let clearButton = UIBarButtonItem(title: "Clear", style: UIBarButtonItemStyle.Plain, target: self, action: "btn_clicked:")
//undo - customView BarButton
var undoBt: UIButton = UIButton(type: .Custom)
undoBt.frame = CGRectMake(0, 0, 30, 30)
undoBt.setImage(UIImage(named: "Undo"), forState: .Normal)
undoBt.addTarget(self, action: "undoBtn:", forControlEvents: .TouchUpInside)
var undoButton: UIBarButtonItem = UIBarButtonItem(customView: undoBt)
/*
a lot more buttons here - deleted for readability
*/
//add buttons to navBar with notButtonSmall for padding
navigationItem.leftBarButtonItems = [ clearButton, notButtonSmall, undoButton, notButtonSmall,pencilButton, notButtonSmall, penButton, notButtonSmall,eraserButton, notButtonSmall,selectButton, notButtonSmall,pasteButton, notButtonSmall,insertButton]
navigationItem.rightBarButtonItems = [ nextButton, notButtonSmall,previousButton, notButtonSmall, palmGuardButton, notButtonSmall, settingsButton, notButtonSmall, filesButton, notButtonSmall, sendButton, notButtonSmall, pagesButton]
// Assign the navigation item to the navigation bar
navigationBar.items = [navigationItem]
// Make the navigation bar a subview of the current view controller
self.view.addSubview(navigationBar)
func btn_clicked(sender: UIBarButtonItem) {
// Do something
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func undoBtn(sender: AnyObject!) {
print("undo")
}
func btn_clicked(sender: AnyObject!) {
print("clear")
}
}