customizing the titleView of navigation bar in iOS7? - ios7

Hello all I want to customize titleView of navigation bar by adding three buttons on navigation bar in iOS7 below is my code but nothing happens
-(void)addCutomButtonsToNavigationController:(UINavigationController *)navigationController
{
UIView *customNavigationTitleView=[[UIView alloc] initWithFrame:CGRectMake(20, 0, 250, 44)];
[customNavigationTitleView setBackgroundColor:[UIColor blackColor]];
UIButton *friendRequestButton=[UIButton buttonWithType:UIButtonTypeCustom];
UIButton *messagesButton=[UIButton buttonWithType:UIButtonTypeCustom];
UIButton *notificationsButton=[UIButton buttonWithType:UIButtonTypeCustom];
[friendRequestButton setImage:[UIImage imageNamed:#"ic_clients.png"] forState:UIControlStateNormal];
[messagesButton setImage:[UIImage imageNamed:#"ic_location.png"] forState:UIControlStateNormal];
[notificationsButton setImage:[UIImage imageNamed:#"ic_message.png"] forState:UIControlStateNormal];
[friendRequestButton setFrame:CGRectMake(0, 10, 40, 40)];
[customNavigationTitleView addSubview:friendRequestButton];
[customNavigationTitleView addSubview:messagesButton];
[customNavigationTitleView addSubview:notificationsButton];
navigationController.navigationItem.titleView =customNavigationTitleView;
}

Try this
-(void)addCutomButtonsToNavigationController:(UINavigationController *)navigationController
{
UIView *customNavigationTitleView=[[UIView alloc] initWithFrame:CGRectMake(40, 0, 160, 44)];
UIButton *friendRequestButton=[UIButton buttonWithType:UIButtonTypeCustom];
UIButton *messagesButton=[UIButton buttonWithType:UIButtonTypeCustom];
UIButton *notificationsButton=[UIButton buttonWithType:UIButtonTypeCustom];
[friendRequestButton setImage:[UIImage imageNamed:#"ic_clients.png"] forState:UIControlStateNormal];
[messagesButton setImage:[UIImage imageNamed:#"ic_location.png"] forState:UIControlStateNormal];
[notificationsButton setImage:[UIImage imageNamed:#"ic_messsage.png"] forState:UIControlStateNormal];
[friendRequestButton setFrame:CGRectMake(10, 0, 40, 40)];
[messagesButton setFrame:CGRectMake(60, 0, 40, 40)];
[notificationsButton setFrame:CGRectMake(110, 0, 40, 40)];
[customNavigationTitleView addSubview:friendRequestButton];
[customNavigationTitleView addSubview:messagesButton];
[customNavigationTitleView addSubview:notificationsButton];
self.navigationItem.titleView =customNavigationTitleView;
}

Related

UIButton changes shape when pressed

-(IBAction)actionButtonPressed:(id)sender {
//Write the code to show the buttons etc like play
[UIView animateWithDuration:0.5
delay:0.0
options:UIViewAnimationOptionAllowAnimatedContent
animations:^{
[self.actionBtn0 setFrame:CGRectMake(25, 25, 100, 100)];
[self.actionBtn1 setFrame:CGRectMake(40, 40, 70, 70)];
[self.actionBtn2 setFrame:CGRectMake(0, 0, 150, 150)];
} completion:^(BOOL finished) {
[UIView animateWithDuration:2.0
delay:0.0
options:UIViewAnimationOptionCurveLinear
animations:^{
playBtn.alpha = 1.0;
}
completion:NULL];
}];
}
I have a button that when the app loads it is large and in the centre of the screen. However, when it is pressed i want it to shrink down in size and move to the top right of the screen. There are actually 3 buttons but they all share the same IBAction shown above.
The problem I am having is that depending on when the button is pressed the images of the buttons morph into random weird shapes that i did not specify. The images should be perfectly circular but they tend to be redrawn elliptically. Any idea how to stop this?
The original dimensions of the buttons before the button is pressed are below.
actionBtn0 = [[UIButton alloc] initWithFrame:CGRectMake(60, 185, 200, 200)];
[actionBtn0 setImage:[UIImage imageNamed:#"bigLineRing.png"]
forState:UIControlStateNormal];
[actionBtn0 addTarget:self action:#selector(actionButtonPressed:)
forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:actionBtn0];
[self.view bringSubviewToFront:actionBtn0];
actionBtn1 = [[UIButton alloc] initWithFrame:CGRectMake(90, 215, 140, 140)];
[actionBtn1 setImage:[UIImage imageNamed:#"smallLineRing.png"]
forState:UIControlStateNormal];
[actionBtn1 addTarget:self action:#selector(actionButtonPressed:)
forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:actionBtn1];
[self.view bringSubviewToFront:actionBtn1];
actionBtn2 = [[UIButton alloc] initWithFrame:CGRectMake(10, 135, 300, 300)];
[actionBtn2 setImage:[UIImage imageNamed:#"theEye.png"] forState:UIControlStateNormal];
[actionBtn2 addTarget:self action:#selector(actionButtonPressed:)
forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:actionBtn2];
[self.view bringSubviewToFront:actionBtn2];
Instead of using this creation of UIButton;
actionBtn1 = [[UIButton alloc] initWithFrame:CGRectMake(90, 215, 140, 140)];
[actionBtn1 setImage:[UIImage imageNamed:#"smallLineRing.png"] forState:UIControlStateNormal];
try to use this:
actionBtn1 = [UIButton buttonWithType:UIButtonTypeCustom];
actionBtn1.frame = CGRectMake(90, 215, 140, 140);
[actionBtn1 setImage:[UIImage imageNamed:#"smallLineRing.png"] forState:UIControlStateNormal];

iPhone UIButton action is not working

I create UIView and then I add UIButton as subview with action, but this action is never called.
Does anybody know why?
Here is my code:
- (void)viewDidLoad{
UIView *roundResultView = [[UIView alloc] initWithFrame:CGRectMake(10, 10, 200, 150)];
UIImageView *_popUpBg = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"NotificationBg.png"]];
_popUpBg.frame = CGRectMake(0, 0, roundViewWidth, roundViewHeight);
UIButton *closeButton = [[UIButton alloc]initWithFrame:CGRectMake(30, 30, 100, 30)];
[closeButton addTarget:self action:#selector(closeAndBack) forControlEvents:UIControlEventTouchUpInside];
[closeButton setTitle:#"Back") forState:UIControlStateNormal];
[roundResultView addSubview:_popUpBg];
[roundResultView addSubview:closeButton];
[self.view addSubview:roundResultView];
}
-(void)closeAndBack{
NSLog(#"closeAndBack"); //never called
}
I have checked this:
NSLog(#"%#", [closeButton allControlEvents]);
NSLog(#"%#", [closeButton allTargets]);
and it prints:
... Sedmica[2192:14f03] 64
... Sedmica[2192:14f03] {(
<ViewController: 0x8631e10>
)}
Update your code with this and kindly let me know :
UIButton *closeButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[closeButton addTarget:self
action:#selector(closeAndBack:)
forControlEvents:UIControlEventTouchDown];
[closeButton setTitle:#"Back"
forState:UIControlStateNormal];
closeButton.frame = CGRectMake(30.0, 30.0, 100.0, 30.0);
[self.view addSubview:closeButton];
- (IBAction)closeAndBack:(UIButton *)button{
NSLog(#"Button clicked.");
}

Buttons that aren't a subview of UIView don't work

If I change it to be a subview of timerView it works fine but nothing else works.
timerView = [[UIView alloc] initWithFrame:CGRectMake(0, 278, 105, 33)];
[self.view addSubview:timerView];
UIImageView *changerBackground = [[UIImageView alloc] init];
[changerBackground setImage:[UIImage imageNamed:#"speedbackground.png"]];
changerBackground.frame = CGRectMake(-12 , -16, 105, 33);
[timerView addSubview:changerBackground];
UIButton *normal = [UIButton buttonWithType:UIButtonTypeCustom];
normal.frame = CGRectMake(40, 8, 22, 22);
[normal setBackgroundImage:[UIImage imageNamed:#"normalwhite.png"] forState:UIControlStateNormal];
[normal setBackgroundImage:[UIImage imageNamed:#"normalred.png"] forState:UIControlStateHighlighted];
[normal addTarget:self action:#selector(quickTest:) forControlEvents:UIControlEventTouchUpInside];
[changerBackground addSubview:normal];
[changerBackground setUserInteractionEnabled:YES];
will solve your issue. As the UIImageView disables the user interaction by default.

Adding CustomView on a NavigationBar

This is my code.
- (void)viewWillAppear:(BOOL)animated
{
customView = [[UIView alloc] init];
UIButton *btnAdd = [[UIButton alloc] initWithFrame:CGRectMake(410, -20, 30, 40)];
[btnAdd setBackgroundColor:[UIColor blackColor]];
[btnAdd setBackgroundImage:[UIImage imageNamed:#"oie_815362uTrAOBMX.png"] forState:UIControlStateNormal];
[btnAdd addTarget:nil action:#selector(addCustomer:) forControlEvents:UIControlEventTouchUpInside];
[customView addSubview:btnAdd];
UIButton *btnJump = [[UIButton alloc] initWithFrame:CGRectMake(450, -20, 30, 40)];
[btnJump setBackgroundColor:[UIColor blueColor]];
[btnJump setBackgroundImage:[UIImage imageNamed:#"oie_8153842yVgkR6XD.jpg"] forState:UIControlStateNormal];
[btnJump addTarget:nil action:#selector(showMenue:) forControlEvents:UIControlEventTouchUpInside];
[customView addSubview:btnJump];
UIButton *btnBarItem = [[UIButton alloc] initWithFrame:CGRectMake(300, -20, 100, 40)];
[btnBarItem setBackgroundColor:[UIColor greenColor]];
[btnBarItem setBackgroundImage:[UIImage imageNamed:#"oie_99342oliBc5Sy.jpg"] forState:UIControlStateNormal];
[btnBarItem addTarget:nil action:#selector(showScanner:) forControlEvents:UIControlEventTouchUpInside];
[customView addSubview:btnBarItem];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, -10, 30, 30)];
[imageView setImage:[UIImage imageNamed:#"oie_972931iW4SHiZU.jpg"]];
[customView addSubview:imageView];
UILabel *mainLabel = [[UILabel alloc] initWithFrame:CGRectMake(50, -20, 80, 40)];
[mainLabel setText:#"Kunden"];
[mainLabel setBackgroundColor:[UIColor lightGrayColor]];
[mainLabel setTextColor:[UIColor blackColor]];
[mainLabel setFont:[UIFont fontWithName:#"Arial" size:18]];
[customView addSubview:mainLabel];
[self.navigationItem setTitleView:customView];
}
- (IBAction)showMenue:(id)sender
{
NewPopUpmenu *options = [[NewPopUpmenu alloc] initWithNibName:#"PopupMenu" bundle:nil];
[options.view setFrame:CGRectMake(720, 0, 300, 200)];
[self.view addSubview:options.view];
}
Note: Problem is that when i press on my "jump" button it doesn't access it's selector Method (showMenue:).
You are specifying a target of nil for each button's target-action pair. If your action method is implemented in the same class, your target should be self:
[btnAdd addTarget:self
action:#selector(addCustomer:)
forControlEvents:UIControlEventTouchUpInside];
// ...
// Same for other buttons...
// ...
Technically, if you specify nil for the target parameter, the responder chain is searched for an object that responds to the action. The problem you are having could possibly be down to the fact that your showeMenu: method isn't the signature that your action is expecting. The action is looking for a method signature that looks like this:
- (void)showeMenu:(id)sender;
Yet you have specified the IBAction return type in the implementation.

Title on a UIButton truncates

I've set a UIButton as the titleView of my navigation bar. However the title seems to truncate no matter how wide the button is. Why?
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setTitle:[[NSDate date] stringWithNSDateFormatterMediumStyle] forState:UIControlStateNormal];
[button setFrame:CGRectMake(0, 0, 150, 40)];
self.navigationItem.titleView = button;
[button setFrame:CGRectMake(0, 0, 250, 40)]; // are you sure your button is wide enough?