我正在使用splitViewController在我的应用中启用滑出菜单,在iOS 9上使用Swift。
除了我无法为我的“菜单”按钮(这是splitViewController的displayModeButtonItem)获取自定义图像外,所有工作都按预期工作。
这是我尝试过的。
#!/usr/bin/perl
use Test::More;
use Test::MockObject;
use Date::Calc;
# scope of mocked Date::Calc
{
my $import;
my $mock = Test::MockObject->new();
$mock->fake_module('Date::Calc', import => sub { $import = caller} );
$mock->fake_new('Date::Calc');
$mock->mock('Days_in_Year', sub {print "how to mock parameter - month to be 2\n"});
# is it possible to mock a parameter? how if possible?
my $days_mock = $mock->Days_in_Year(2015,6);
ok($days_mock == 59, "expect: 59, got: $days_mock\n");
}
# unmocked module and methods
my $days_in_year = Date::Calc::Days_in_Year(2015,6);
ok($days_in_year == 181, "expect: 181, got: $days_in_year\n");
done_testing(2);
这个版本是我尝试使用自定义图像而不是文本标题,但我只是得到一个灰色框,图像应该是。
override func viewDidLayoutSubviews() {
// This will create a properly-working menu barbuttonitem, but is of course
// a text based title.
navigationItem.leftBarButtonItem = UIBarButtonItem(title: "≡",
style: UIBarButtonItemStyle.Plain,
target: splitViewController!.displayModeButtonItem().target,
action: splitViewController!.displayModeButtonItem().action)
有什么建议吗? 感谢
答案 0 :(得分:1)
您需要制作UIButton
并在其中设置图片,并将其设置为-viewDidLoad
中的leftBarButtonItem:
let menuButton = UIButton(type: .Custom)
menuButton.frame = CGRectMake(0, 0, 29, 29)
menuButton.setImage(UIImage(named:"29x29"), forState: .Normal)
menuButton.addTarget(self, action: "menuPressed:", forControlEvents: .TouchUpInside)
let barButton = UIBarButtonItem(customView: menuButton)
navigationItem.leftBarButtonItem = barButton
并以单独的方法执行您的操作:
func menuPressed(sender: AnyObject) {
//show menu
}