Buttons are not seen on the view - ios

I have added buttons programmatically to my view, but only toolbar is visible on the view when i test on the simulator.
What am i doing wrong?
I have defined my ui elements in header file, and synthesized in a main file and then initialized in the viewDidLoad method.
But there is something wrong.
My code is like below;
#import "ViewController.h"
#implementation ViewController
#synthesize iAmLost, iAmLooking, about, howToUse,appsFromKodAtolye, exit;
- (void)viewDidLoad
{
[super viewDidLoad];
toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0,0, 320.0, 40.0)];
iAmLost = [[UIButton alloc]initWithFrame:CGRectMake(0,0, 200.0, 10.0)];
iAmLooking = [[UIButton alloc ]initWithFrame:CGRectMake(0, 0, 200.0, 10.0) ];
[iAmLooking addTarget:self
action:#selector(iAmLookingScreen)
forControlEvents:UIControlEventTouchUpInside];
about = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 200.0, 10.0)];
howToUse = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 200.0, 10.0)];
exit = [[UIBarButtonItem alloc] initWithTitle:#"Exit" style:UIBarButtonItemStyleBordered target:self action:#selector(exitApp:)];
NSArray *buttons = [[NSArray alloc]
initWithObjects:exit, nil];
toolbar.items = buttons;
[self.view addSubview:iAmLost];
[self.view addSubview:iAmLooking];
[self.view addSubview:about];
[self.view addSubview:toolbar];
}

You are placing the buttons underneath the toolbar, you add each item at 0,0. As the toolbar is the last item you add to the subview it will appear on top of all the buttons and therefore you will not be able to see the buttons.
The frame is made up of 4 parts x co-ordinate, y co-ordinate, height, width
Try changing the x and y co-ordinates for these
For example
toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0,0, 320.0, 40.0)];
iAmLost = [[UIButton alloc]initWithFrame:CGRectMake(0,100, 200.0, 10.0)];
iAmLooking = [[UIButton alloc ]initWithFrame:CGRectMake(0, 200, 200.0, 10.0) ];
You should also note that you currently have not set any style for the buttons or any titles. It might be worth setting titles or a background colour so that you can actually see what buttons you have placed on screen and tell which is which.

All your buttons are below the toolbar since you're adding the toolbar as the last subview.
You have to add your buttons as subviews of the toolbar. Also be aware that you're positioning all the buttons at the same position.

You have total four buttons and one tool bar. The frame of tool bar is totally correct ie
(0, 0, 320.0, 40.0)
But for buttons you are giving same coordinates for all, so it is overlapping. Better you will change button coordinates.
And if you want to add these buttons on tool bar then set coordinate properly and use
[toolBar addSubView: button];

Simple, just check the origin x & y for each Button
UIToolbar* toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0,0, 320.0, 40.0)];
UIButton *iAmLost = [[UIButton alloc]initWithFrame:CGRectMake(5,50, 200.0, 10.0)];
iAmLost.backgroundColor = [UIColor redColor];
UIButton * iAmLooking = [[UIButton alloc ]initWithFrame:CGRectMake(0, 70, 200.0, 10.0) ];
[iAmLooking addTarget:self
action:#selector(iAmLookingScreen)
forControlEvents:UIControlEventTouchUpInside];
UIButton * about = [[UIButton alloc]initWithFrame:CGRectMake(0, 90, 200.0, 10.0)];
about.backgroundColor = [UIColor greenColor];
UIButton * howToUse = [[UIButton alloc]initWithFrame:CGRectMake(0, 120, 200.0, 10.0)];
howToUse.backgroundColor = [UIColor blackColor];
UIBarButtonItem* exit = [[UIBarButtonItem alloc] initWithTitle:#"Exit" style:UIBarButtonItemStyleBordered target:self action:#selector(exitApp:)];
NSArray *buttons = [[NSArray alloc]
initWithObjects:exit, nil];
toolbar.items = buttons;
[self.view addSubview:iAmLost];
[self.view addSubview:iAmLooking];
[self.view addSubview:howToUse];
[self.view addSubview:about];
[self.view addSubview:toolbar];

Related

How to add UIButton as bottomView to UIView?

I have a UIView class, and in xib also I have a MainView which is a UIView. I want to add UIButton as bottomView to the View.I need to give bottom space 16 between MainView and bottomView. How do I do it?
self.bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, self.MainView.bounds.size.height +30, self.MainView.bounds.size.width, 40.0)];
[self.bottomView setBackgroundColor:[UIColor greenColor]];
self.selectBtn= [UIButton buttonWithType: UIButtonTypeRoundedRect];
[self.selectBtn setFrame:CGRectMake(0,20,100,40)];
[self.selectBtn setTitle:#"Select" forState:UIControlStateNormal];
[self.selectBtn addTarget:self action:#selector(selectButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
[self.bottomView addSubview:self.selectBtn];
[self addSubview:self.bottomView];
Expected result
Output from above code
Please find you have added button on this frame
self.bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, self.MainView.bounds.size.height +30, self.MainView.bounds.size.width, 40.0)];
Here you have made the y as height + 30 so it is not visible to the view.
You should use this.
self.bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, self.MainView.bounds.size.height - 40, self.MainView.bounds.size.width, 40.0)];
Also if you want 16 pixel gap you should the view above it with the same approach.
Please let me know if you need any more help

Wrong UIImageView dimensions on navigationItem

I am trying to add an image on navigation bar but the dimensions are wrong.
UIImageView* menuButton=[[UIImageView alloc]initWithFrame:CGRectMake(4, 7, 20, 20)];
menuButton.image=[UIImage imageNamed: Model.icon.MapList];
UIBarButtonItem *accountBarItem = [[UIBarButtonItem alloc] initWithCustomView:menuButton];
self.navigationItem.leftBarButtonItem = accountBarItem;
It seams that I don't have any control on the dimensions of the image, because when I change the values of initWithFrame nothing changes.
Also this problem appears only on iPhones greater than iPhone 5 model.
What am I missing?
You can just use simple button and set image for it.
UIButton *b = [UIButton buttonWithType:UIButtonTypeCustom];
[b setFrame:CGRectMake(0.0f, 0.0f, 25.0f, 25.0f)];
[b addTarget:self action:#selector(randomMsg) forControlEvents:UIControlEventTouchUpInside];
[b setImage:[UIImage imageNamed: Model.icon.MapList] forState:UIControlStateNormal];
UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:b];
self.navigationItem.leftBarButtonItem = item;
If you have wrong aspect ratio , you can use
[[b imageView] setContentMode: UIViewContentModeScaleAspectFit];
I'd recommend adding a border to your image view so you can see what's going on.
Here's what you're currently experiencing (using the imageView directly as the custom view of the bar button item):
- (void)_addImageViewToLeftButtonItemWithoutOuterView {
UIImageView *profileImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"Homer.jpg"]];
[profileImageView setFrame:CGRectMake(0, 0, 40, 40)];
profileImageView.layer.cornerRadius = 20.0f;
profileImageView.layer.masksToBounds = YES;
profileImageView.clipsToBounds = YES;
profileImageView.layer.borderColor = [UIColor blackColor].CGColor;
profileImageView.layer.borderWidth = 1.0f;
UIBarButtonItem *rbi = [[UIBarButtonItem alloc] initWithCustomView:profileImageView];
self.navigationItem.leftBarButtonItem = rbi;
}
This is what it looks like without aspect fit:
And with aspect fit set:
And this is what I believe you want:
- (void)_addImageViewToLeftButtonItemWithOuterView {
UIView *profileView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
profileView.layer.borderColor = [[UIColor redColor] CGColor];
profileView.layer.borderWidth = 2.0f;
UIImageView *profileImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"Homer.jpg"]];
profileImageView.contentMode = UIViewContentModeScaleAspectFit;
[profileImageView setFrame:CGRectMake(0, 0, 40, 40)];
profileImageView.layer.cornerRadius = 20.0f;
profileImageView.layer.masksToBounds = YES;
profileImageView.clipsToBounds = YES;
profileImageView.layer.borderColor = [UIColor blackColor].CGColor;
profileImageView.layer.borderWidth = 1.0f;
[profileView addSubview:profileImageView];
UIBarButtonItem *lbi = [[UIBarButtonItem alloc] initWithCustomView:profileView];
self.navigationItem.leftBarButtonItem = lbi;
}
Which results in this:
As you probably noticed, the sorta trick here is that you don't want to add the image view directly as the custom view within initWithCustomView of the UIBarButtonItem. Instead you want to use an outer view to hold the imageView, then you can adjust the frame of your subview to move it around.
Here's one more example if you wanted the image further to the right (obviously you won't want the border for the outer image view, but I've just left it there for demonstration purposes):
- (void)_addImageViewToLeftButtonItemWithOuterView {
// adjust this to 80 width (it always starts at the origin of the bar button item
UIView *profileView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 80, 40)];
profileView.layer.borderColor = [[UIColor redColor] CGColor];
profileView.layer.borderWidth = 2.0f;
UIImageView *profileImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"Homer.jpg"]];
profileImageView.contentMode = UIViewContentModeScaleAspectFit;
// and adjust this to be offset by 40 (move it to the right some)
[profileImageView setFrame:CGRectMake(40, 0, 40, 40)];
profileImageView.layer.cornerRadius = 20.0f;
profileImageView.layer.masksToBounds = YES;
profileImageView.clipsToBounds = YES;
profileImageView.layer.borderColor = [UIColor blackColor].CGColor;
profileImageView.layer.borderWidth = 1.0f;
[profileView addSubview:profileImageView];
UIBarButtonItem *lbi = [[UIBarButtonItem alloc] initWithCustomView:profileView];
self.navigationItem.leftBarButtonItem = lbi;
}

How to add two toolbars over keyboard

I'm able to add a single toolbar over keyboard but i don't know how to add two consecutive toolbars over keyboard for single textView.
I'm able to add one tool bar just like below
But i want to do just like the below attachment.
There is no built in way of doing this. You will need to roll your own UIView / UIToolbar and place it above the keyboards first UIToolBar - you can use the system notifications to listen to UIKeyboard events and the adjust the second UIToolbar's frame property accordingly.
You can use UIToolbar and customize it to add your buttons into it. You can add buttons like this.
-(void)addToolBarOnKeyBordOnTextField:(UITextView *)textview
{
if (!viewToolbar) {
viewToolbar = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 110)];
[viewToolbar setBackgroundColor:[UIColor clearColor]];
UIToolbar * numberToolbar1 = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0,self.view.frame.size.width, 50)];
numberToolbar1.barStyle = UIBarStyleBlack;
numberToolbar1.translucent = YES;
UIButton *buttonTopLeft = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 60, 50)];
[buttonTopLeft setTitle:#"Clear" forState:UIControlStateNormal];
[numberToolbar1 addSubview:buttonTopLeft];
UIButton *buttonTopRight = [[UIButton alloc] initWithFrame:CGRectMake(self.view.frame.size.width - 60, 0, 60, 50)];
[buttonTopRight setTitle:#"Done" forState:UIControlStateNormal];
[numberToolbar1 addSubview:buttonTopRight];
[viewToolbar addSubview:numberToolbar1];
UIToolbar *numberToolbar2 = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 60,self.view.frame.size.width, 50)];
numberToolbar2.barStyle = UIBarStyleBlack;
numberToolbar2.translucent = YES;
UIButton *buttonBottomLeft = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 60, 50)];
[buttonBottomLeft setTitle:#"Clear" forState:UIControlStateNormal];
[numberToolbar2 addSubview:buttonBottomLeft];
UIButton *buttonBottomRight = [[UIButton alloc] initWithFrame:CGRectMake(self.view.frame.size.width - 60, 0, 60, 50)];
[buttonBottomRight setTitle:#"Done" forState:UIControlStateNormal];
[numberToolbar2 addSubview:buttonBottomRight];
[viewToolbar addSubview:numberToolbar2];
}
[textview setInputAccessoryView:viewToolbar];
}
Remember to call this method in textViewShouldBeginEditing. You can add remaining buttons and set the frames as done in code.

How to add multiple bar buttons to navigation bar

Initially i have created a dynamic view called 'mainView'.In that,i have added Navigation bar.After that, i Want to add 2 right bar buttons to the navigation bar.If i code like this,its shows view with navigation bar.but its not showing right bar button.
1.Is it possible to add navigation bar without navigation controller?
2.Why the right bar button is not showing?
//mainView Creation
UIView *mainView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
mainView.backgroundColor =[UIColor grayColor];
[self.view addSubview:mainView];
//NavigationBar
UINavigationBar *NavigationBar = [[UINavigationBar alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 45)];
NavigationBar.backgroundColor = [UIColor orangeColor];
[mainView addSubview:NavigationBar];
//rightbarView Creation
UIView *rightbarView = [[UIView alloc] init];
rightbarView.backgroundColor = [UIColor orangeColor];
//powerCut Button
UIButton *powerCutbutton = [[UIButton alloc] initWithFrame:CGRectMake(150, 20, 50, 14.01)];
[rightbarView addSubview:powerCutbutton];
UIImage *powercutimage = [UIImage imageNamed:#"powercut.jpg"];
[powerCutbutton setImage:powercutimage forState:UIControlStateNormal];
// addConnection button
UIButton *addConnectionbutton = [[UIButton alloc] initWithFrame:CGRectMake(210, 20, 50, 14.01)];
[rightbarView addSubview:addConnectionbutton];
UIImage *addimage = [UIImage imageNamed:#"add.png"];
[addConnectionbutton setImage:addimage forState:UIControlStateNormal];
// Barbutton item
UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:rightbarView];
//[self.navigationItem setRightBarButtonItems:[NSArray arrayWithObjects:powerCutbutton, addConnectionbutton, nil]];
// setting rightbar button item
self.navigationItem.rightBarButtonItem = item;
create an array of bar buttons. and setLeftBarButtonItems not setLeftBarButtonItem as :
UIBarButtonItem * barbtn1;
UIBarButtonItem * barbtn2;
UIBarButtonItem * barbtn3;
[self.navigationItem setLeftBarButtonItems:#[barbtn1,barbtn2,barbtn3]];
or
[self.navigationItem setRightBarButtonItems:#[barbtn1,barbtn2,barbtn3]];
hope it will helpful for you

Add a sticky view underneath the Navigation Bar

I've seen a few other examples, but I haven't seen anything that actually sticks. What I want is essentially what a tableViewHeader does. I have a tableViewController with a navigation bar. I want to put a view into place that shows some search criteria in a small bar directly below the navbar. But I need it to stick when the user swipes to view the results.
I've tried adding a UIView as a subview to the navigation bar, but it always sits at the top of the Navigation Bar, overlaying everything.
Here's what worked:
// Create a custom navigation view
_navigationView = [[UIView alloc] init];
_navigationView.backgroundColor = [UIColor whiteColor];
_navigationView.frame = CGRectMake(0.0f,
self.navigationController.navigationBar.frame.origin.y + 44.0f,
self.view.frame.size.width,
30.0f);
// Create bottom border for the custom navigation view
_navigationViewBorder = [[UIView alloc] init];
_navigationViewBorder.backgroundColor = [UIColor darkGrayColor];
_navigationViewBorder.tag = 1;
_navigationViewBorder.frame = CGRectMake(0.0f,
self.navigationController.navigationBar.frame.origin.y + 74.0f,
self.view.frame.size.width,
0.5f);
// Add labels for date, results, and the time range
_dateDisplay = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 70, 15)];
[_dateDisplay setFont:[UIFont fontWithName:#"HelveticaNeue" size:13]];
[_dateDisplay setText:#""];
_timeRangeDisplay = [[UILabel alloc] initWithFrame:CGRectMake(98, 0, 135, 15)];
[_timeRangeDisplay setFont:[UIFont fontWithName:#"HelveticaNeue-Bold" size:13]];
[_timeRangeDisplay setText:#""];
_resultsDisplay = [[UILabel alloc] initWithFrame:CGRectMake(240, 0, 80, 15)];
[_resultsDisplay setFont:[UIFont fontWithName:#"HelveticaNeue" size:13]];
[_resultsDisplay setText:#""];
[_navigationView addSubview: _dateDisplay];
[_navigationView addSubview: _timeRangeDisplay];
[_navigationView addSubview: _resultsDisplay];
// Add two views to the navigation bar
[self.navigationController.navigationBar.superview insertSubview:_navigationView belowSubview:self.navigationController.navigationBar];
[self.navigationController.navigationBar.superview insertSubview:_navigationViewBorder belowSubview:_navigationView];
Why not add the view to your viewController's view, and set up its constraints so that it sits just below the navigationBar, but above the tableView?
If it's only supposed to be there some of the time, you can animate its constraints to show/hide it.

Resources