默认系统项的自定义背景

时间:2013-03-18 11:56:05

标签: ios uibarbuttonitem uibarbuttonitemstyle

我想使用默认的栏按钮项目,如

UIBarButtonItem *editFavoritesButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:self action:@selector(startEndEditing:)];

问题是我想改变这些按钮的背景但是

[editFavoritesButton setBackgroundImage:[UIImage imageNamed:@"barbuttonPlain.png"] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];

在这种情况下没有诀窍。

更新:

这个想法不是创建一个自定义按钮,因为你需要处理这种情况的翻译。另一方面,我想要用户撰写和其他类型的默认图像。

5 个答案:

答案 0 :(得分:1)

将您的代码替换为......

[editFavoritesButton setBackgroundImage:[UIImage imageNamed:@"barbuttonPlain.png"] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
像这样......

[editFavoritesButton setImage:[UIImage imageNamed:@"barbuttonPlain.png"]];

答案 1 :(得分:0)

使用此代码创建自定义UIBarButtonItem:

UIButton *mybutton = [UIButton buttonWithType:UIButtonTypeCustom];
[mybutton setFrame:CGRectMake(0.0f, 0.0f, 25.0f, 25.0f)];
[mybutton addTarget:self action:@selector(mybuttonFunction) forControlEvents:UIControlEventTouchUpInside];
[mybutton setImage:[UIImage imageNamed:@"myImage.png"] forState:UIControlStateNormal];

UIBarButtonItem *random = [[UIBarButtonItem alloc] initWithCustomView:mybutton];

希望这会对你有所帮助。

答案 2 :(得分:0)

你可以这样试试如果你有任何问题可以问......

UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
backButton.frame = CGRectMake(0, 0, 83, 44);
backButton.adjustsImageWhenHighlighted=NO;
[backButton addTarget:self action:@selector(backMethod)
       forControlEvents:UIControlEventTouchDown];
[backButton setImage:[UIImage imageNamed:@"img.png"]
              forState:UIControlStateNormal];


UIBarButtonItem *barLeftInfoButton = [[UIBarButtonItem alloc] initWithCustomView:backButton];
self.navigationItem.leftBarButtonItem = barLeftInfoButton;

答案 3 :(得分:0)

首先添加自定义按钮,然后将其添加到UIBarButtonItem。

UIImage *buttonImage = [UIImage imageNamed:@"mainButton.png"];

    //create the button and assign the image
self.yourButton = [UIButton buttonWithType:UIButtonTypeCustom];
[self.yourButton setFrame:CGRectMake(5,7,buttonImage.size.width,buttonImage.size.height)];

[self.yourButton setBackgroundImage:buttonImage forState:UIControlStateNormal];
self.yourButton.titleLabel.font = [UIFont boldSystemFontOfSize:12];
[self.yourButton setTitle:@"title" forState:UIControlStateNormal];
[self.yourButton addTarget:self action:@selector(startEndEditing:) forControlEvents:UIControlEventTouchUpInside];

    // add image view
[self.navBar addSubview:self.yourButton];


    //create a UIBarButtonItem with the button as a custom view
UIBarButtonItem *customBarItem = [[UIBarButtonItem alloc] initWithCustomView:self.yourButton];
self.navigationItem.leftBarButtonItem = customBarItem;

答案 4 :(得分:0)

请尝试使用这个......

UIButton *editFavoritesButton = [[UIButton alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 55.0f, 30.0f)];
[editFavoritesButton setBackgroundColor:[UIColor clearColor]];
[editFavoritesButton setBackgroundImage:[UIImage imageNamed:@"barbuttonPlain.png"] forState:UIControlStateNormal];
[editFavoritesButton addTarget:self action:@selector(startEndEditing:) forControlEvents:UIControlEventTouchUpInside];

UIBarButtonItem *leftBtn = [[UIBarButtonItem alloc] initWithCustomView:editFavoritesButton];
[self.navigationItem setLeftBarButtonItem:leftBtn];