Interface Builder adjusting navigation item incorrectly - ios

I have been messing with this for a while and I can't figure out what would be causing this issue. I have a UIViewController embedded in a UINavigationController and on the generated UIViewController has two UINavigationItems:
1:left button is a default Done button with no image - appears fine
2:right button is a plain button with a 30x30 image
These buttons are not connected to any IBOutlet and are not altered within the Swift code. However when the code is compiled and is running on a simulator the right button is pushed down the screen. If the image is removed and standard text is added the button appears fine.
Any suggestions would be greatly appreciated, thank you
Environment details:
MacOSX 10.11.2 , Xcode 7.2

This is what I do in objective-c to set the buttons to a specific side.
UIImage *image = [UIImage imageNamed:#"Notification.png"];
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
[button setBackgroundImage:image forState:UIControlStateNormal];
[button addTarget:self
action:#selector(goToNotifiationView:)
forControlEvents:UIControlEventTouchUpInside];
button.adjustsImageWhenHighlighted = NO;
UIBarButtonItem *rightButton =
[[UIBarButtonItem alloc] initWithCustomView:button];
self.navigationItem.rightBarButtonItem = rightButton;

Related

iOS11 UIBarButton position issue

Just installed Xcode9 and working on my project.
I notice the buttons on the navigation bar are now positioned differently now.
The Pictures below are navigation bar under different spec and Xcode:
iPhone7plus current deploy under xcode8
iPhone8plus in Xcode9
iPhone8 in Xcode9
As you can see, in iPhone7+ in current deploy with previous Xcode, the buttons are correctly positioned on the left and right hand side.
However, when I run in xCode9, the buttons and the logo are slightly indent to the right.
Can anyone help me out how I can get the buttons position correctly to the left and right, and have the logo position correctly in the centre ?
Here are the codes set up for the navigation bar and buttons set up:
UIView * leftNaviNaviButtonView = [[UIView alloc] initWithFrame:CGRectMake(0, 0,delegate.windowWidth, 44)];
[leftNaviNaviButtonView setBackgroundColor:[UIColor grayColor]];
UIBarButtonItem * negativeSpacer = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil]autorelease];
negativeSpacer.width = -16;
searchbarBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[searchbarBtn setFrame:CGRectMake(0,0,50,44)];
[searchbarBtn setImage:[UIImage imageNamed:#"search_btn.png"] forState:UIControlStateNormal];
[searchbarBtn.imageView setContentMode:UIViewContentModeScaleAspectFit];
[leftNaviNaviButtonView addSubview:searchbarBtn];
UIImageView * logoImageView = [UIImageView new];
[logoImageView setFrame:CGRectMake((delegate.windowWidth-170)/2,0,170,40)];
[logoImageView setImage:[UIImage imageNamed:#"app_headerlogo.png"]];
[logoImageView setContentMode:UIViewContentModeScaleAspectFit];
[leftNaviNaviButtonView addSubview:logoImageView];
UIButton *advanceSearch = [UIButton buttonWithType:UIButtonTypeCustom];
[advanceSearch setFrame:CGRectMake(delegate.windowWidth-50,0,50,44)];
[advanceSearch setImage:[UIImage imageNamed:#"sear ch_adv.png"] forState:UIControlStateNormal];
[advanceSearch.imageView setContentMode:UIViewContentModeScaleAspectFit];
[leftNaviNaviButtonView addSubview:advanceSearch];
[self.navigationItem setLeftBarButtonItems:[NSArray arrayWithObjects:negativeSpacer,[[[UIBarButtonItem alloc] initWithCustomView:leftNaviNaviButtonView]autorelease], nil]];
[leftNaviNaviButtonView release];
Thank you so much in advance.

IOS 8 - Navigation bar buttons issue

My app has simple viewControll(XIB, no storyboard) with 6 sub views
When user click on buttons sub view bring in front of current view (by using call [self.view bringSubviewToFront:usageView];)
App working fine on IOS 6 and 7 but I facing issue on IOS 8.
When I click on buttons for bring sub view in front sub view come up with like below image
Navigation bar right side buttons "Done" show improper, its hide only in iOS 8
Please help me for this issue I am new in IOS development. If your required any more information please comment.
Add this in viewDidLoad method of this viewController and customise it like you want
UIImage *backButtonImage = [UIImage imageNamed:#"Back_A.png"];
UIImage *backButtonImage1 = [UIImage imageNamed:#"Back_B.png"];
UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
[backButton setImage:backButtonImage
forState:UIControlStateNormal];
[backButton setImage:backButtonImage1
forState:UIControlStateSelected];
[backButton setImage:backButtonImage1
forState:UIControlStateHighlighted];
backButton.frame = CGRectMake(0, 0, 55, 45);
[backButton addTarget:self
action:#selector(goBack)
forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *backBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
self.navigationItem.rightBarButtonItem = backBarButtonItem;
With frame you can show the button where you want.

MFMailComposeViewController customize

I am customizing MFMailComposeViewController it is working fine in ios 5.0 and ios 5.1 but not working properly in ios 6. The custom send and cancel button does not appear in mailcontroller.
My code is :
sendBtn = mailer.navigationBar.topItem.rightBarButtonItem;
cancelBtn = mailer.navigationBar.topItem.leftBarButtonItem;
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:#"navigation.png"] forBarMetrics:UIBarMetricsDefault];
UINavigationItem *mailVCNavItem = [mailer.navigationBar.items objectAtIndex:0];
// Get the old bar button item to fetch the action and target.
UIBarButtonItem *oldCancelBarButton = [mailVCNavItem leftBarButtonItem];
// Create your new custom bar button item.
// In my case I have UIButton with image set as a custom view within a bar button item.
UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
[backButton setImage:[UIImage imageNamed:#"cancel-button-hover.png"] forState:UIControlStateNormal];
[backButton addTarget:oldCancelBarButton.target action:oldCancelBarButton.action forControlEvents:UIControlEventTouchUpInside];
[backButton setFrame:CGRectMake(0, 0, 55, 28)];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
UIButton *sendbtn = [UIButton buttonWithType:UIButtonTypeCustom];
[sendbtn setImage:[UIImage imageNamed:#"send-btnComment.png"] forState:UIControlStateNormal];
[sendbtn addTarget:self action:#selector(sendMail:) forControlEvents:UIControlEventTouchUpInside];
[sendbtn setFrame:CGRectMake(0, 0, 55, 28)];
self.navigationItem.rightBarButtonItem =[[UIBarButtonItem alloc] initWithCustomView:sendbtn];
MFMailComposeViewController and the related Facebook and Twitter sharing views are implemented through remote view controllers in iOS 6. This means that the controllers are run in another process and it is no longer possible to customize them by accessing their properties or subviews directly. You can still do so through UIAppearence though, but what you are trying to with replacing the buttons is no longer possible in iOS 6.

limit the touchable area of UIbutton

This is how I set button navigation bar
UIButton *addEditButton = [UIButton buttonWithType:UIButtonTypeCustom];
[addEditButton setImage:[UIImage imageNamed:#"edit.png"] forState:UIControlStateNormal];
[addEditButton setFrame:CGRectMake(0, 0, 62, 31)]; used frame same as image
[addEditButton addTarget:self action:#selector(EditTable) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *addEdit = [[UIBarButtonItem alloc] initWithCustomView:addEditButton];
self.navigationItem.leftBarButtonItem =addEdit;
Everything works perfectly but button get pressed when I touch out side of it. How to solve this is there any way so it get pressed only if I touch on it
This is that image
apple has set this thing in way so user can navigate smoothly. it is advisable not to make such design in which you are putting buttons near navigationBarButton. there are some way to do it but its not good to change this kind of things. its just like reply to message and delete message both button are near beside to each other
I believe that you got this issue because the image you are setting is smaller thant the actual size of the button. please either make the button size smaller or provide a bigger image.
I hope this helps you.
It seems it works like that only, still I came up with a solution.
One is you can hide your navigation bar and use a toolbar indeed.
Other is you can add another button after that and set it's enabled property to FALSE.
I don't know whether this is proper or not, but seems to fulfill your requirement at least.
Here is the code:
UIButton *addEditButton = [UIButton buttonWithType:UIButtonTypeCustom];
[addEditButton setFrame:CGRectMake(0, 0, 62, 31)];
[addEditButton setImage:[UIImage imageNamed:#"edit.png"] forState:UIControlStateNormal];
[addEditButton addTarget:self action:#selector(EditTable) forControlEvents:UIControlEventTouchUpInside];
UIButton *addEditButton1 = [UIButton buttonWithType:UIButtonTypeCustom];
[addEditButton1 setFrame:CGRectMake(63, 0, 30, 31)];
UIBarButtonItem *addEdit = [[UIBarButtonItem alloc] initWithCustomView:addEditButton];
UIBarButtonItem *addEdit1 = [[UIBarButtonItem alloc] initWithCustomView:addEditButton1];
addEdit1.enabled = FALSE;
NSMutableArray *buttonArray=[[NSMutableArray alloc]initWithCapacity:2];
[buttonArray addObject:addEdit];
[buttonArray addObject:addEdit1];
self.navigationItem.leftBarButtonItems =buttonArray;

RightBarButtonItem Does Not Appear in Navigation Bar

This is bugging the heck out of me! I'm just trying to programmatically add a right-side "flash" button to my navigation bar, and for whatever reason, I can't make it appear. My code is below. Can anyone help me? Thanks in advance.
//Note: flashButton is declared as a UIButton property in .h, and synthesized in .m
UIView *flashContainer = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 36, 36)];
[flashButton addTarget:self action:#selector(toggleTorch) forControlEvents:UIControlEventTouchUpInside];
[flashButton setBackgroundImage: [UIImage imageNamed:#"FlashIconInactive_small.png"] forState:UIControlStateNormal];
[flashButton setBackgroundImage: [UIImage imageNamed:#"FlashIconActive_small.png"] forState:UIControlStateHighlighted];
[flashContainer addSubview:flashButton];
[flashButton sizeToFit];
UIBarButtonItem *flashButtonItem = [[UIBarButtonItem alloc] initWithCustomView:flashContainer];
scannerVC.delegate = self;
scannerVC.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:#selector(cancelTapped)];
[[scannerVC navigationItem] setRightBarButtonItem:flashButtonItem];
Is flashButton connected to a button in a xib or storyboard via an IBOutlet? If not, you'll need to create an instance of UIButton using +[UIButton buttonWithType:].
Another test to try is to set a breakpoint on your first line (the addTarget: one) and enter po flashButton in the debugger. What's the output? If it's nil, then your button doesn't exist and needs to be created.

Resources