Skip to content

Instantly share code, notes, and snippets.

@artanisdesign
Created December 25, 2013 22:31
Show Gist options
  • Save artanisdesign/8127588 to your computer and use it in GitHub Desktop.
Save artanisdesign/8127588 to your computer and use it in GitHub Desktop.
in iOS7, with Titanium, if you set rightNavButton or leftNavButton, with a custom button or view.. etc, it has extra spacing on the left or right side, which is really annoying, if you want the button snap to the edge. here is a small hack, if you need the view positioned to the left or right side correctly.
//from line 502
-(void)setRightNavButton:(id)proxy withObject:(id)properties
{
ENSURE_UI_THREAD_WITH_OBJ(setRightNavButton,proxy,properties);
if (properties == nil) {
properties = [self valueForKey:@"rightNavSettings"];
}
else {
[self setValue:properties forKey:@"rightNavSettings"];
}
if (controller!=nil &&
[controller navigationController] != nil)
{
ENSURE_TYPE_OR_NIL(proxy,TiViewProxy);
[self replaceValue:proxy forKey:@"rightNavButton" notification:NO];
if (proxy==nil || [proxy supportsNavBarPositioning])
{
// detach existing one
UIBarButtonItem *item = controller.navigationItem.rightBarButtonItem;
if ([item respondsToSelector:@selector(proxy)])
{
TiViewProxy* p = (TiViewProxy*)[item performSelector:@selector(proxy)];
[p removeBarButtonView];
}
if (proxy!=nil)
{
// add the new one
BOOL animated = [TiUtils boolValue:@"animated" properties:properties def:NO];
//only ios7+ and if the view has the value set
if([proxy valueForKey:@"rightNavNegativeSpace"] != nil && [TiUtils isIOS7OrGreater]){
UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
negativeSpacer.width = [[proxy valueForKey:@"rightNavNegativeSpace"] floatValue];
[controller.navigationItem setRightBarButtonItems:[NSArray arrayWithObjects:negativeSpacer, [proxy barButtonItem], nil] animated:animated];
} else{
[controller.navigationItem setRightBarButtonItem:[proxy barButtonItem] animated:animated];
}
}
else
{
controller.navigationItem.rightBarButtonItem = nil;
}
}
else
{
NSString *msg = [NSString stringWithFormat:@"%@ doesn't support positioning on the nav bar",proxy];
THROW_INVALID_ARG(msg);
}
}
else
{
[self replaceValue:[[[TiComplexValue alloc] initWithValue:proxy properties:properties] autorelease] forKey:@"rightNavButton" notification:NO];
}
}
-(void)setLeftNavButton:(id)proxy withObject:(id)properties
{
ENSURE_UI_THREAD_WITH_OBJ(setLeftNavButton,proxy,properties);
if (properties == nil) {
properties = [self valueForKey:@"leftNavSettings"];
}
else {
[self setValue:properties forKey:@"leftNavSettings"];
}
if (controller!=nil && [controller navigationController] != nil)
{
ENSURE_TYPE_OR_NIL(proxy,TiViewProxy);
[self replaceValue:proxy forKey:@"leftNavButton" notification:NO];
if (proxy==nil || [proxy supportsNavBarPositioning])
{
// detach existing one
UIBarButtonItem *item = controller.navigationItem.leftBarButtonItem;
if ([item respondsToSelector:@selector(proxy)])
{
TiViewProxy* p = (TiViewProxy*)[item performSelector:@selector(proxy)];
[p removeBarButtonView];
}
controller.navigationItem.leftBarButtonItem = nil;
if (proxy!=nil)
{
// add the new one
BOOL animated = [TiUtils boolValue:@"animated" properties:properties def:NO];
if([proxy valueForKey:@"leftNavNegativeSpace"] != nil && [TiUtils isIOS7OrGreater]){
UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
negativeSpacer.width = [[proxy valueForKey:@"leftNavNegativeSpace"] floatValue];
[controller.navigationItem setLeftBarButtonItems:[NSArray arrayWithObjects:negativeSpacer, [proxy barButtonItem], nil] animated:animated];
} else{
[controller.navigationItem setLeftBarButtonItem:[proxy barButtonItem] animated:animated];
}
}
else
{
controller.navigationItem.leftBarButtonItem = nil;
}
}
else
{
NSString *msg = [NSString stringWithFormat:@"%@ doesn't support positioning on the nav bar",proxy];
THROW_INVALID_ARG(msg);
}
}
else
{
[self replaceValue:[[[TiComplexValue alloc] initWithValue:proxy properties:properties] autorelease] forKey:@"leftNavButton" notification:NO];
}
}
//if you put a custom view/button/etc into rightNavButton, set rightNavNegativeSpace (if right side, obviously left for leftside)
//correct spacing is -16, both side
var rightView = $.UI.create('View', {
classes : "rightview",
rightNavNegativeSpace : -16
});
$.win.rightNavButton = rightView;
@ibrida
Copy link

ibrida commented Mar 3, 2015

Titanium has two ways to add buttons to the navigation bar: the leftNavButton and leftNavButtons (the second one takes an array of views). Will your solution work in this last case too?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment