are there any way to get one 3D button like this:
what i want to do, is something like this:
MK3dBUTTON *3dButton = [[MK3dBUTTON alloc] initWithMapView:self.mapView];
[3dButton setTarget:self];
UIBarButtonItem *3dbarButton = [[UIBarButtonItem alloc] initWithCustomView:3dButton];
[self.uiTollbar setItems:#[3dbarButton];
so how can i take this 3d button directly in code?
thanks
My solution:
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button addTarget:self action:#selector(changeMapViewButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
[button setFrame:CGRectMake(0, 0, 50, 30)];
[button setTitle:#"3D" forState:UIControlStateNormal];
[button setTitleColor:self.view.tintColor forState:UIControlStateNormal];
[button setTitle:#"3D" forState:UIControlStateHighlighted];
[button setTitleColor:[self.view.tintColor colorWithAlphaComponent:0.15] forState:UIControlStateHighlighted];
[button.titleLabel setFont:[UIFont fontWithName:#"HelveticaNeue-Thin" size:25]];
UIBarButtonItem *changeMapViewItem = [[UIBarButtonItem alloc] initWithCustomView:button];
This is what it looks like:
I tried it with:
UIBarButtonItem *changeMapViewItem = [[UIBarButtonItem alloc] initWithTitle:#"3D" style:UIBarButtonItemStylePlain target:self action:#selector(changeMapViewButtonPressed:)];
[changeMapViewItem setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont fontWithName:#"HelveticaNeue-Thin" size:25], NSFontAttributeName,nil] forState:UIControlStateNormal];
But then the Text does not align with the icons in the UIToolbar.
HTH
Dominik
Related
On a view controller, I have a back button with custom image via this code
UIButton *backButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 32, 32)];
[backButton setImage:[UIImage imageNamed:#"left207.png"] forState:UIControlStateNormal];
[backButton addTarget:self action:#selector(goToParent) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *backBtn = [[UIBarButtonItem alloc] initWithCustomView:backButton];
self.navigationItem.leftBarButtonItem = backBtn;
self.navigationItem.rightBarButtonItem = rightButton;
But when I run the app, there is no pushed sort of effect. It doesn't turn dark for a second.
You need to set another image for highlighted state:
[backButton setImage:[UIImage imageNamed:#"highlightedButton"] forState:UIControlStateHighlighted];
I create two UIBarButtonItem in the code below. Later I add them to rightBarButtonItems yet only one of the two buttons shows up (the one I put first on the list).
I thought perhaps that the title's view (which is not set) would be in front of the rightBarButtonItems, since the Apple docs says "If there is not enough room to display all of the items in the array, those that would overlap the title view (if present) or the buttons on the left side of the bar are not displayed."
But that doesn't seem to be it either, since the view's frame = {0,0}{0,0}
I can't figure out what I'm doing wrong. Can anybody tell me how to get all rightBarButtonItems to show? Code:
UIBarButtonItem *shareButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:#selector(shareAction:)];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setFrame:CGRectMake(0, 0, 30, 30)];
[button setBackgroundImage:[UIImage imageNamed:#"custom-bar-button.png"] forState:UIControlStateNormal];
[button addTarget:self action:#selector(barButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *wishlistButton = [[UIBarButtonItem alloc] initWithCustomView:button];
[self.navigationItem setRightBarButtonItems:[[NSArray alloc] initWithObjects:shareButton, wishlistButton, nil]];
Use UIBarButtonItem:
UIBarButtonItem *button = [UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"custom-bar-button.png"] style:UIBarButtonItemStylePlain target:self action:#selector(barButtonTapped:)];
instead of:
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setFrame:CGRectMake(0, 0, 30, 30)];
[button setBackgroundImage:[UIImage imageNamed:#"custom-bar-button.png"] forState:UIControlStateNormal];
[button addTarget:self action:#selector(barButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
UIButton *addButton = [UIButton buttonWithType:UIButtonTypeCustom];
[addButton setImage:[UIImage imageNamed:#"addButton.png"] forState:UIControlStateNormal];
[addButton addTarget:self action:#selector(addSubProperty:) forControlEvents:UIControlEventTouchUpInside];
[addButton setFrame:CGRectMake(0, 0,51,44)];
UIBarButtonItem *firstBarButton = [[UIBarButtonItem alloc] initWithCustomView:addButton];
UIButton *deleteButton = [UIButton buttonWithType:UIButtonTypeCustom];
[deleteButton setImage:[UIImage imageNamed:#"deleteButton.png"] forState:UIControlStateNormal];
[deleteButton addTarget:self action:#selector(deleteProperty:) forControlEvents:UIControlEventTouchUpInside];
[deleteButton setFrame:CGRectMake(0, 0,50,44)];
UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
[negativeSpacer setWidth:-10];
UIBarButtonItem *secondBarButton = [[UIBarButtonItem alloc]initWithCustomView:deleteButton];
NSArray *navBtnArr = [[NSArray alloc] initWithObjects:negativeSpacer,firstBarButton,secondBarButton, nil];
self.navigationItem.rightBarButtonItems = navBtnArr;
After this i got this result ,as you guys can see i did able to manage move my first barbutton left byt that negativeSpacer but i donot know how to reduce space between those two button.
and by doing this my back also goes disappear any idea will really appreciated .
Using Xcode 6.4, This worked beautifully for me with 1 line of code!
https://stackoverflow.com/a/26469607/3634990
self.myBarButtonItem.imageInsets = UIEdgeInsetsMake(0, 25, 0, -25);
Note, this moves the image location, but not the touch area, so it's a great fix only if you want to move the image just a little bit.
self.navigationItem.leftBarButtonItem.tintColor = [UIColor whiteColor];
I have done above,but it doesn't work.The color is still as same as the background color!
Create button and substitute
UIBarButtonItem *leftBarButton = [[UIBarButtonItem alloc] initWithCustomView:self.leftBarButton];
self.navigationItem.leftBarButtonItem = leftBarButton;
or
+ (UIBarButtonItem*)itemWithNormalImage:(UIImage*)normalImage
pressedImage:(UIImage*)pressedImage
target:(id)target
action:(SEL)action
{
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:normalImage
forState:UIControlStateNormal];
[button setImage:pressedImage
forState:UIControlStateSelected];
[button setImage:pressedImage
forState:UIControlStateHighlighted];
button.frame = CGRectMake(0, 0, normalImage.size.width, normalImage.size.height);
[button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem* item=[[UIBarButtonItem alloc] initWithCustomView:button];
return item;
}
Text color:
UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithTitle:#"done" style:UIBarButtonItemStyleBordered target:self action:nil];
[item setTitleTextAttributes:#{NSForegroundColorAttributeName:[UIColor whiteColor]} forState:UIControlStateNormal];
self.navigationItem.leftBarButtonItems = #[item];
is there any way to change the button image inside UIToolbar?
I am actually using this way but it doesn't work.
[myButton setImage:[UIImage imageNamed:#"blue.png"]];
For your information, "myButton" is an IBOulet UIBarButtonItem.
#property (nonatomic, strong) IBOutlet UIBarButtonItem *myButton;
Use the following cole. i hope it will use to you.
UIImage *image = [UIImage imageNamed:#"buttonImage.png"];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.bounds = CGRectMake( 0, 0, image.size.width, image.size.height );
[button setImage:image forState:UIControlStateNormal];
[button addTarget:myTarget action:#selector(myAction) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
Thanks,.
// add images to bar button item of toolbar in iphone
to set button type as custum:
button=[UIButton buttonWithType: UIButtonTypeCustom];
[button setImage:[UIImage imageNamed:#"myImage.png"] forState:UIControlStateNormal];
[button addTarget:self action:#selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];
[button setFrame:CGRectMake(0, 0, 40, 40)];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
// add the image on toolbar:
[toolbar insertSubview:[[[UIImageView alloc]
initWithImage:[UIImage imageNamed:#"image.png"]] autorelease] atIndex:1];