how set images on buttons of navigation bar?

谁说我不能喝 提交于 2020-01-25 11:38:26

问题


I need to put images on add,cancel and back buttons of navigation bar. I have done with add button but not getting for back button.

even with this add button image it shows back blue color custom button.how to set for this?

UIBarButtonItem *addButton=[[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"bar_add_button.png"] 
    style:UIBarButtonSystemItemAdd target:self               
    action:@selector(addNote)];  

回答1:


// Initialize the UIButton
UIImage *buttonImage = [UIImage imageNamed:@"buttonImage.png"];
UIButton *aButton = [UIButton buttonWithType:UIButtonTypeCustom];
[aButton setImage:buttonImage forState:UIControlStateNormal];
aButton.frame = CGRectMake(0.0, 0.0, buttonImage.size.width, buttonImage.size.height);

// Initialize the UIBarButtonItem
UIBarButtonItem *aBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:aButton];

// Set the Target and Action for aButton
[aButton addTarget:self action:@selector(aButtonClicked:) forControlEvents:UIControlEventTouchUpInside];

// Then you can add the aBarButtonItem to the UINavigationBar
...
self.navigationItem.leftBarButtonItem = aBarButtonItem;

// Release buttonImage
[buttonImage release];


来源:https://stackoverflow.com/questions/6435118/how-set-images-on-buttons-of-navigation-bar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!