How to add two toolbars over keyboard - ios

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.

Related

how to resize image icon in navigation item bar in swift?

I have 2 navigation bar items, but the size is too big, I want to make it smaller. how do i manage the size of them? should i edit them in the interface builder or should i manage them programmatically in viewDidLoad ?
I had given all the image size needed in the image asset, but it doesnt work
You have to take custom view for the navigationItem
take a button and give frame as per your requirement
set an image to the button
assign a button as customView to UIBarButtonItem
try following code ..it may help you
let customButton = UIButton.init(frame: CGRect.init(x: 0, y: 0, width: 30, height: 30))
customButton.setImage(UIImage.init(named:"imageName"), for: .normal)
self.navigationItem.rightBarButtonItem = UIBarButtonItem.init(customView: customButton)
Thank you
To use resized images in bar buttons, bellow code will help you:
UIView *customView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
[customView setBackgroundColor:[UIColor clearColor]];
CGRect iconRect = CGRectMake(((40 - 28)/2), ((40 - 28)/2), 28, 28);
UIImageView *imageIcon = [[UIImageView alloc] initWithFrame: iconRect];
imageIcon = [UIImage imageNamed:imageName];
[customView addSubview: imageIcon];
UIButton *tranparentButton = [UIButton buttonWithType:UIButtonTypeCustom];
[subscribeButton setFrame:CGRectMake(0, 0, 40, 40)];
[customView addSubview: tranparentButton];
[tranparentButton addTarget:self action:#selector(cancelButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *rightBarButton = [[UIBarButtonItem alloc] initWithCustomView: customView];
self.navigationItem.rightBarButtonItem = rightBarButton;
Thanks

searchBar‘s frame changed after clicked cancel button

I add my searchBar(left) and a button(right) as subViews to a UIView.Then set the UIView as the TableHeaderView.But after I end searching and click the cancel button back to the tableview,the searchBar will totally cover the tableHeaderView and can't see the button added.And I viewed the layer,the button still there,just covered by the expanding searchbar.Anyone know how to resolve this?Thanks in advance!
self.searchController = [[UISearchController alloc]initWithSearchResultsController:nil];
self.searchController.searchResultsUpdater = self;
UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, WinWidth, 40)];
self.searchController.searchBar.frame = CGRectMake(0, 0, WinWidth-50, 40);
UIButton *filterBtn = [[UIButton alloc]initWithFrame:CGRectMake(WinWidth-30, 10, 20, 20)];
filterBtn setBackgroundImage:[UIImage imageNamed:#"arrow_expend_down"] forState:UIControlStateNormal];
[view addSubview:self.searchController.searchBar]; [self.searchController.searchBar addSubview:filterBtn];
self.searchController.searchBar.barTintColor = [self colorWithRGB:0xf2f2f2 alpha:1];
[view addSubview:filterBtn];
view.backgroundColor =self.searchController.searchBar.barTintColor; self.tableView.tableHeaderView = view;
try adding : tableView.tableHeaderView.contentMode = UIViewContentModeCenter;
Try this
[self.searchController.searchBar sizeToFit];

How to have two different colors for two UIBarButtonItem in navigation bar at the same time?

Are there any standard methods for having two different colors for two UIBarButtonItems on a navigationBar at the same time?
This could be achieve using custom UIButton, e.g.:
UIButton *confirmBtn = [[UIButton alloc] init];
confirmBtn.titleLabel.font =[UIFont boldSystemFontOfSize:16.0f];
[confirmBtn setTitle:#"Confirm" forState:UIControlStateNormal];
confirmBtn.titleEdgeInsets = UIEdgeInsetsMake(0, 0, 0, -40);
[confirmBtn setTitleColor:[ColorSetter colorWithHexString:#"ffd420"] forState:UIControlStateNormal];
confirmBtn.frame = CGRectMake(0, 0, 80, 50);
[confirmBtn addTarget:self action:#selector(DoneClicked:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *confirmBtnBarItem = [[UIBarButtonItem alloc] initWithCustomView:confirmBtn];
self.navigationItem.rightBarButtonItem = confirmBtnBarItem;

How to customize UINavigationBar

I'm struggling in custom UINavigationBar. The reason i want to customize is because there is same action that can be occur in different view controller. I post some images as an example.
As you can see from the images above. I think you get what i mean. I have been trying and searching, but i don't get the answer i wanted. Furthermore, i have tried using UIView to accomplish this, it worked, but i don't think it is correct way to do this. Therefore, i need help from you guys to lead me to the right path. :) :) Thank you.
You can set custom title view to the navigation bar like this way.
//To hide default back button
self.navigationItem.hidesBackButton=YES;
//Title View for Navigation
UIView *navTitle1 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44)];
[navTitle1 setBackgroundColor:[UIColor lightGrayColor]];
//Left View button and separator
UIButton *backBarButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 50, 44)];
[backBarButton setTitle:#"Back" forState:UIControlStateNormal];
UIView *ttlview1 = [[UIView alloc] initWithFrame:CGRectMake(51, 0, 1, 44)];
[ttlview1 setBackgroundColor:[UIColor darkGrayColor]];
//Center view with two buttons and seprator for them
UIView *middleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 121, 44)];
[middleView setBackgroundColor:[UIColor clearColor]];
[middleView setCenter:navTitle1.center];
UIButton *postBarButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 60, 44)];
[postBarButton setTitle:#"Post" forState:UIControlStateNormal];
UIView *ttlview2 = [[UIView alloc] initWithFrame:CGRectMake(middleView.frame.size.width/2, 0, 1, 44)];
[ttlview2 setBackgroundColor:[UIColor darkGrayColor]];
UIButton *memeBarButton = [[UIButton alloc] initWithFrame:CGRectMake((middleView.frame.size.width/2)+1, 0, 60, 44)];
[memeBarButton setTitle:#"Meme" forState:UIControlStateNormal];
[middleView addSubview:ttlview2];
[middleView addSubview:postBarButton];
[middleView addSubview:memeBarButton];
//Right button with seprator before that
UIView *ttlview3 = [[UIView alloc] initWithFrame:CGRectMake(self.view.frame.size.width-71, 0, 1, 44)];
[ttlview3 setBackgroundColor:[UIColor darkGrayColor]];
You can refer THIS for more detail.
You can create 2 custom UIBarButtonItem from 2 views, using this document:
From Apple documentation:
Initializes a new item using the specified custom view.
- (id)initWithCustomView:(UIView *)customView
Parameters customView: A custom view representing the item.
Return Value Newly initialized item with the specified properties.
Left bar item: 1 view contains 3 buttons: Project, Kiara Paradize, Artist Impression.
Right bar item: 1 view contains 4 buttons: Sign up | Login (Profile button when user logged in) | Hamburger menu
Then:
self.navigationItem.leftBarButtonItem = UIBarButtonItem from left
Custom View
self.navigationItem.rightBarButtonItem = UIBarButtonItem from right Custom View

UITextField rightViewMode not working correctly

I need to make a custom clear button on a UITextField, so I am using the rightView. Code is below:
UIImage *clearImage = [[UIImage imageNamed:#"search-clear.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0.0,0.0, 0.0, 0.0)];
UIImageView *clearImageView = [[UIImageView alloc]initWithImage:clearImage];
UIButton *clearButton = [UIButton buttonWithType:UIButtonTypeCustom];
clearButton.frame = clearImageView.frame;
[clearButton setBackgroundImage:clearImage forState:UIControlStateNormal];
clearButton.backgroundColor = [UIColor clearColor];
self.emailTextField.rightViewMode = UITextFieldViewModeWhileEditing;
self.emailTextField.rightView = clearButton;
self.emailTextField.clearButtonMode = UITextFieldViewModeNever;
However, the button is not showing up at the correct times. It shows when the text field is in focus only with text length 0. As soon as I start typing it disappears. I need to figure out how to replace and duplicate the clear button, so that it shows when the string is at least 1 character.
you can't display both at the same time , but you can do it like this
UITextField * textfield = [[UITextField alloc] initWithFrame:CGRectMake(10, 100, 300, 40)];
[textfield setBorderStyle:UITextBorderStyleRoundedRect];
UIImageView * imgvw = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"search.jpeg"]];
[imgvw setFrame:CGRectMake(0, 0, 30, 30)];
[textfield setRightView:imgvw];
[textfield setRightViewMode:UITextFieldViewModeUnlessEditing];
[textfield setClearButtonMode:UITextFieldViewModeWhileEditing];
[self.view addSubview:textfield];

Resources