NavigationItem LeftBarButtonItem Custome View Auto Layaut - ios

I am trying to set a costume navigationItem.leftBarButtonItem from a nib, but i get an weird behaviour in landscape.
EDIT:
To be clear the view contains a button and a label,this is why i am using a costume view, both button and label have constraints.
self.buttonView =//init;
[self.buttonView setFrame:CGRectMake(0, 0, 36, 36)];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:self.buttonView];
[self.buttonView setBackgroundColor:[UIColor redColor]];
//you will see why

Seems that autolayout is trying to satisfy some constraints thus its expanding your button's frame.
To fix this set the contentHugging priority to required for your button:
[self.buttonView setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
Try that and let me know if it works

This is the only workaround i could find asap, if someone have a better idea please feel free to share
-(void)viewWillLayoutSubviews{
[super viewWillLayoutSubviews];
[self setMenuButtonFrame];
}
-(void)setMenuButtonFrame
{
self.menuView.frame = CGRectMake(self.menuView.frame.origin.x,
self.menuView.frame.origin.y,
self.navigationController.navigationBar.frame.size.height-8,
self.navigationController.navigationBar.frame.size.height-8);
}

Related

setting the frame of a customView in UIBarButtonItem

I am doing
mapButton *topMapButton = [[mapButton alloc] init];
[topMapButton setSize:CGSizeMake(70, 30)];
topMapButton.frame = CGRectMake(0, STATUS_HEIGHT +7, 70, 30);
UIBarButtonItem *barIcon = [[UIBarButtonItem alloc] initWithCustomView: topMapButton];
[topMapButton addTarget:self action:#selector(openMap:) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.rightBarButtonItem = barIcon;
where mapButton is a UIButton subclass, with preset look, nothing else
No matter what frame I set for topMapButton, it keeps being in (0,0) and not in the rightBarButtonItem usual place.
Note that using autolayout programatically to set the place works for placing, but crashes when I push another controller in the stack...
Am I doing anything wrong?
Look at this answer
Shortly, you can set edge inset. override alignmentRectInsets for your uibutton subclass and change top and left insets.
- (UIEdgeInsets)alignmentRectInsets {
UIEdgeInsets insets = UIEdgeInsetsMake(top_value, left_value, bottom_value, right_value);
return insets;
}
OK found the problem..
It seems that UIBarButtonItem doesn't really care what frame the customView has, since it will set it anyway.
however, my custom button MUST have a custom type, otherwise it will mess the frame and keep it (0,0) of the application screen.
self = [UIButton buttonWithType:UIButtonTypeCustom];

Place floating UIButton on top of UICollectionView

I'm desperately trying to place a UIButton on top of my UICollectionView. The goal is something like the check-in button from the new foursquare app.
My code currently looks like so:
UIButton *floatingButton = [[UIButton alloc] init];
[floatingButton setFrame:CGRectMake(320, 150, 50, 50)];
[floatingButton setBackgroundColor:[UIColor blackColor]];
[self.collectionView addSubview:floatingButton];
[self.collectionView bringSubviewToFront:floatingButton];
The problem is the button isn't anywhere.
you probably want to add the button to the collectionView's parent view.
most likely self.view, but its hard to say for sure without seeing more code.
instead of
[self.collectionView addSubview:floatingButton];
you probably want
[self.view addSubview:floatingButton];
I had the same problem. Add your button to collectionView superView, might be self.view.
[self.view addSubview:floatingButton];
and set the button as firstResponder.
[floatingButton becomeFirstResponder];

Adding UIToolbar programmatically

What am I doing wrong here ? I just don't see the toolbar at the bottom of screen Here's my code.
CGRect rect = self.view.frame;
UIToolbar *toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(rect.origin.x,
rect.size.height-44,
rect.size.width,
44)];
self.bottomToolbar = toolBar;
[toolBar release];
[_bottomToolbar setBackgroundImage:nil
forToolbarPosition:UIToolbarPositionBottom
barMetrics:UIBarMetricsDefault];
[self.view addSubview:_bottomToolbar];
You need to set the toolbar's autosizingMask to the "flexible top margin" value.
Also, your code deals with the toolBar variable, the bottomToolbar property, and the _bottomToolbar ivar. Either use the property or the ivar. It's confusing to use both like you are.
there is missing one line, its sizeToFit or makeKeyAndVisible()
Just look in the example in the ViewControllerProgrammingGuide

navigationbar, title, center alignment through code

I have a number of of xibs where I manipulate the title of the navigation bar through code:
titleLabel.text = #"custom Title";
titleLabel.textAlignment = UITextAlignmentCenter;
self.navigationItem.titleView = titleLabel;
The problem I have is that in some cases I add to that bar a right or left button, or both, and whenever I do that the text alignment ends up being a little bit off and not centered proerly. How can I fix that?
Get the width of the barButton and subtract it from the label's width.
You can determine your bar button's frame through this method:
Get the width of a UIBarButtonItem
i.e.
NSInteger barButtonWidth = //determined through method above ^^^^^
titleLabel.frame = CGRectMake(titleLabel.frame.origin.x, titleLabel.frame.origin.y, titleLabel.size.width-barButtonWidth, titleLabel.size.height);
I kind of found out a workaround it.
By adding the the buttons before setting the title view, xcode automatically sets the alignment properly. the order is important.
This worked for me
UILabel *labelNav;
- (void)viewDidLoad {
[super viewDidLoad];
labelNav=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, 150, 20)];
labelNav.font=[UIFont fontWithName:boldFont size:15];
labelNav.textColor=barTintColor;
labelNav.text=kBoostBusinessHoursScreenTitle;
labelNav.textAlignment=NSTextAlignmentCenter;
labelNav.center=self.navigationController.navigationBar.center;
CGRect framelabel=labelNav.frame;
framelabel.origin.y=12;
labelNav.frame=framelabel;
[self.navigationController.navigationBar addSubview:labelNav];
}
-(void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
[labelNav removeFromSuperview];
}

Table footer view buttons

I have some signup form that had email and login text fields as table cells and signup button as button in footer view.
That all functioned superb, here is the code
frame = CGRectMake(boundsX+85, 100, 150, 60);
UIButton *signInButton = [[UIButton alloc] initWithFrame:frame];
[signInButton setImage:[UIImage imageNamed:#"button_signin.png"] forState:UIControlStateNormal];
[signInButton addTarget:self action:#selector(LoginAction) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.hidesBackButton = TRUE;
self.tableView.tableFooterView.userInteractionEnabled = YES;
self.tableView.tableFooterView = signInButton;
self.view.backgroundColor = [UIColor blackColor];
My question is how to add another button next to this one.
By using [self.tableView.tableFooterView addSubview:...] buttons are not shown and if I use some other UIView, place buttons there and then say that the footerView is that UIView, I see the buttons, but am unable to press them.
I hope that this isn't too confusing and that you understand my problem.
Take a look at this question: UIButtons on tableFooterView not responding to events
your first try is wrong, if you are doing as you as saying you are trying to add a button to the subview of a button:
first you
...
self.tableView.tableFooterView = signInButton;
...
and then later
...
[self.tableView.tableFooterView addSubview:...]
...
but tableFooterView is signInButton. So that is why that is not working.
your second try is correct and the answer yonanderson pointed you should work out and is the correct way to do this, you just need to :
[yourButtonsView addSubView:button1];
[yourButtonsView addSubView:button2];
yourButtonsView.userInteractionEnabled = YES;
self.tableView.tableFooterView = yourButtonsView;
self.tableView.tableFooterView.userInteractionEnabled = YES;
Humm, I didn't read this close enough before answering. I think you need to set the container UIView's userInteractionEnabled property then as you tried, set the footerView to the container with the subviews.
It took me forever to figure out why my footer's buttons were not calling the designated actions. Finally I discovered that I needed to adjust my footer's height. This fixed it for me:
-(CGFloat) tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
return footerView.bounds.size.height;
}

Resources