I'm having a problem with reading taps on navigation bar (I need to open dropdown menu by tapping on title field, just like in telegram and etc. but not in swift)
I know that basically its unreadable, and already've tried with taprecognizer, but it didn't work for me any good.
Now my menu opens with rightbarbutton - that's ugly.
How can I deal with it?
Now string that drops menu (placed in viewDidLoad) looks like:
navbar = [[UIBarButtonItem alloc]initWithTitle:#"navbar" style:UIBarButtonItemStylePlain target:self action:#selector(navigationTitleTapGestureAction:)];
and a part that declares button:
NSArray *buttons = #[navbar <<...a few more buttons...>>];
self.navigationItem.rightBarButtonItems = buttons;
EDIT: additional code from the comments:
- (IBAction)navigationTitleTapGestureAction:(id)sender {
automaticDisappearanceCanceled_ = YES;
if (menuToolbarVisible_) {
[self hideMenuAnimated:YES];
}
else {
[self showMenuAnimated:YES];
}
}
- (void)showMenuAnimated:(BOOL)animated {
[self.view layoutIfNeeded];
self.toolbarTopLayoutConstraint.constant = 0.f;
self.menuToolbar.hidden = NO;
[UIView animateWithDuration:animated ? ToolbarMenuAnimationDuration : 0.f animations:^{
[self.view layoutIfNeeded];
} completion: ^(BOOL finished) {
}];
menuToolbarVisible_ = YES;
}
- (void)hideMenuAnimated:(BOOL)animated {
[self.view layoutIfNeeded];
self.toolbarTopLayoutConstraint.constant = -ToolbarMenuHeight;
[UIView animateWithDuration:animated ? ToolbarMenuAnimationDuration : 0.f animations: ^{
[self.view layoutIfNeeded];
} completion:^(BOOL finished) {
self.menuToolbar.hidden = YES;
}];
menuToolbarVisible_ = NO;
}
As far is I know you just want to get taps on the text on the center of navigation bar you can do this by adding a UIButton in the titleView like this :-
UIButton *centerButton=[[UIButton alloc]initWithFrame:CGRectMake(0, 0, 44.0, 44.0)];
[centerButton setTitle:#"Center" forState:UIControlStateNormal];
[centerButton addTarget:self action:#selector(action) forControlEvents:UIControlEventTouchDown];
self.navigationItem.titleView=centerButton;
Related
I have made a view on another view and make a movable label form right to left, problem is that label moves on whole view and i want to show it only on second view not on mainview.. how to hide that label from particular view??
here is my code
- (void)viewDidLoad {
[super viewDidLoad];
self.movelabel = [[UILabel alloc]initWithFrame:CGRectMake(200, 0, 200, 30)];
self.movelabel.text = #"This is my music line";
[self.moveview addSubview:self.move];
[NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:#selector(LabelAnimation) userInfo:nil repeats:YES];
}
-(void)LabelAnimation
{
[UIView animateWithDuration:3.0f delay:0.0f options:UIViewAnimationOptionTransitionNone animations:^{
self.move.frame = CGRectMake(-200, 0, 200, 60);
} completion:^(BOOL finished)
{
self.move.frame = CGRectMake(200, 0, 200, 60);
}];
}
Thankyou.
If self.move is label then set clipsToBounds property of its superview to yes
i.e
self.move.superview.clipsToBounds = YES;
It will clip if self.move goes out of bounds of its superview.
Hope it helps :)
to hide UILabel
use :-
yourLbl.hidden = YES;
[yourLbl setHidden:YES];
or to remove
[yourLbl removeFromSuperview];
hope it may help you.
I have one viewcontroller. At top i am having one textfield.And also i am having one bar button item(add button) at navigation bar. In viewdidload initially my texfield will be hidden. when user press that bar button (add button) my textfield should be visible. now its working well.
Needer:
when user pressed (first time) my bar button (add button) should visible. then again when user press that same bar button item (add button) its should be again hidden.i did in visible .but i don't know again how to make its hidden...i am very beginner please help me out
i know that its should be done in if statement.but i don't know what condition should use ...help me this
Thanks in advance !
#import "ViewController.h"
#interface ViewController () {
UIBarButtonItem *addButton;
}
#end
#implementation ViewController
#synthesize tableView;
#synthesize textField;
- (void)viewDidLoad {
[super viewDidLoad];
addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:#selector(addButtonPressed:)];
self.navigationItem.rightBarButtonItem = addButton;
txtField.hidden = YES;
}
- (void)addButtonPressed:(id)sender
{
txtField.hidden = NO;
}
#end
Ok for that you have to define a Bool variable, change the status of that Boolean variable on the click of your button like this
#import "ViewController.h"
#interface ViewController () {
Bool isShowingTF;
UIBarButtonItem *addButton;
}
#end
#implementation ViewController
#synthesize tableView;
#synthesize textField;
- (void)viewDidLoad {
[super viewDidLoad];
isShowingTF = NO;
addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:#selector(addButtonPressed:)];
self.navigationItem.rightBarButtonItem = addButton;
txtField.hidden = YES;
}
- (void)addButtonPressed:(id)sender
{
if (isShowingTF) {
txtField.hidden = YES;
} else {
txtField.hidden = NO;
}
isShowingTF = ! isShowingTF;
}
#end
you can do this in many ways just like set BOOL for yes:No condition, else identify with Tags else use some common NSString like
#interface ViewController () {
UIBarButtonItem *addButton;
NSString *GettouchStr;
}
- (void)viewDidLoad {
[super viewDidLoad];
addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:#selector(addButtonPressed:)];
self.navigationItem.rightBarButtonItem = addButton;
txtField.hidden = YES;
GettouchStr=#"hidden";
}
- (void)addButtonPressed:(id)sender
{
if ([GettouchStr isEqualToString:#"hidden"])
{
[UIView animateWithDuration:0.3 animations:^{
txtField.alpha = 1;
} completion: ^(BOOL finished) {
txtField.hidden = NO;
GettouchStr=#"UNhidden";
}];
}
else
{
[UIView animateWithDuration:0.3 animations:^{
txtField.alpha = 0;
} completion: ^(BOOL finished) {
txtField.hidden = YES;
GettouchStr=#"hidden";
}];
}
}
Choice - 2 for additional animation
- (void)addButtonPressed:(id)sender
{
if ([GettouchStr isEqualToString:#"hidden"])
{
txtField.alpha = 1;
[UIView animateWithDuration:2.f delay:0.f options:UIViewAnimationOptionCurveEaseIn animations:^{
txtField.alpha = 0;
} completion:^(BOOL finished) {
[UIView animateWithDuration:2.f delay:0.f options:UIViewAnimationOptionCurveEaseInOut animations:^{
txtField.alpha = 1;
txtField.hidden = NO;
GettouchStr=#"UNhidden";
} completion:nil];
}];
}
else
{
txtField.alpha = 0;
[UIView animateWithDuration:2.f delay:0.f options:UIViewAnimationOptionCurveEaseIn animations:^{
txtField.alpha = 1;
} completion:^(BOOL finished) {
[UIView animateWithDuration:2.f delay:0.f options:UIViewAnimationOptionCurveEaseInOut animations:^{
txtField.alpha = 0;
txtField.hidden = YES;
GettouchStr=#"hidden";
} completion:nil];
}];
}
}
Choice-3
see this link may be help with you
In Swift 4, If you have only one bar button item in the right side you can use this one,
self.navigationItem.rightBarButtonItem = nil; //To Hide
self.navigationItem.rightBarButtonItem = barButtonItem //To show
Suppose if you have multiple bar button in the right side, for example suppose you have two bar button items(search button and filter button) in the right side of your navigation item. And now you have to hide only search button, you can use like,
self.navigationItem.rightBarButtonItems = [filterItem]
Now what happening is, you can completely hide the search button from the navigation item and the filter item comes in the place of search item
Then if you want to show the hided bar button,
self.navigationItem.rightBarButtonItems = [searchItem, filterItem]
Now in the navigationItem searchItem comes as the first item, filterItem comes as the second item.
I know how to show keyboard accessory with UITextField.
textField.inputAccessoryView = keyboardToolBar;
But what if I don't have a UITextField but a button. How do I show a keyboard that has a toolbar?
The toolbar itself will contain a UITextField, which I will have to be first responder programmatically.
But how would that work?
Update
-(void)createKeyboardToolBar
{
self.keyboardToolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 42)];
self.textField = [[UITextField alloc] initWithFrame:CGRectMake(8, 6, 250, 30)];
UIBarButtonItem *textBtn = [[UIBarButtonItem alloc] initWithCustomView:self.textField ];
UIBarButtonItem *postBtn = [[UIBarButtonItem alloc]initWithTitle:#"Post" style:UIBarButtonItemStyleBordered target:self action:#selector(postComment)];
[self.keyboardToolBar setItems: [NSArray arrayWithObjects:textBtn,postBtn,nil]];
self.textField.inputAccessoryView = self.keyboardToolBar;
}
So following the suggestions, I come up with the code above. But when I click the button that is supposed to make self.textField the first responder, the call is made, but the keyboard or the toolbar does not appear. (I use NSLog)
for your button add action as buttonClicked. so when you click on the button show keyboard by using becomeFirstResponder for your textfield.
- (void)buttonClicked {
[myTextField becomeFirstResponder];
}
Alloc the textfield,toolbar globally, and add it to the toolbar.
In the button action write the 'become firstresponder' for the textfield.
Here is the solution
- (IBAction)keyboardaction:(id)sender {
self.keyToolbar_.hidden=NO;
CGRect lframe = self.keyToolbar_.frame;
lframe.origin.y = [[UIScreen mainScreen] bounds].size.height-<keyboardheight>;
[self.entryTxfld_ becomeFirstResponder];
[UIView animateWithDuration:0.3
delay:0
options:UIViewAnimationOptionCurveEaseInOut
animations:^ {
self.keyToolbar_.frame = lframe;
}completion:^(BOOL finished) {
}];
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
CGRect lframe = self.keyToolbar_.frame;
lframe.origin.y = [[UIScreen mainScreen] bounds].size.height;
[UIView animateWithDuration:0.6
delay:0
options:UIViewAnimationOptionCurveEaseInOut
animations:^ {
self.keyToolbar_.frame = lframe;
}completion:^(BOOL finished) {
}];
[textField resignFirstResponder];
return YES;
}
In the XIB for View add toolbar the bottom and to the toolbar added the textfield.For reference find the image.
I am trying to Display a an Image when the user Taps on the UIImageView. But before the user Taps the UIImageView the Image should not be shown, and after a few seconds the Image should disappear again. Does anyone know how to do this? I read through couple of Threads but they do not work with the latest Xcode as it appears. Thanks for your help and time.
Udate
Well, my code now looks like this:
-(void)imageTapped:(UITapGestureRecognizer*)recognizer
{
recognizer.view.alpha=0.0;
((UIImageView*)recognizer.view).image = [UIImage imageNamed:#"twingo_main.png"];
[UIView animateWithDuration:1.0 delay:2.0 options:0 animations:^{
recognizer.view.alpha=1.0;
} completion:^(BOOL finished) {
recognizer.view.alpha=0.0;
((UIImageView*)recognizer.view).image = nil;
recognizer.view.alpha=1.0;
}];
}
- (void)viewDidLoad
{
UIImageView *hiddenImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 30, 20, 20)];
hiddenImage.userInteractionEnabled=YES;
[hiddenImage addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(imageTapped:)]];
[self.view addSubview:hiddenImage];
Well, now my question is, how do I need to set up the UIImageView in the View Controller?
In your UIViewController's viewDidLoad method:
...
UIImageView *hiddenImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 30, 20, 20)];
hiddenImage.userInteractionEnabled=YES;
[hiddenImage addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(imageTapped:)]];
[self.view addSubview:hiddenImage];
...
UITapGestureRecognizer Handler:
-(void)imageTapped:(UITapGestureRecognizer*)recognizer
{
recognizer.view.alpha=0.0;
((UIImageView*)recognizer.view).image = [UIImage imageNamed:#"imageName"];
[UIView animateWithDuration:1.0 delay:2.0 options:0 animations:^{
recognizer.view.alpha=1.0;
} completion:^(BOOL finished) {
recognizer.view.alpha=0.0;
((UIImageView*)recognizer.view).image = nil;
recognizer.view.alpha=1.0;
}];
}
Set the image in the UIImageView to nil initially. Add a tap gesture recognizer to the UIImageView that, when fired, sets the image and starts a timer. When the timer completes, set your image back to nil.
Here is an another way to handle above with NSTimer..
-(void)imageTapped:(UITapGestureRecognizer*)recognizer
{
CustomPopUpView *lCustomPopUpView = [[CustomPopUpView alloc]init];
//Added your imageview to Custom UIView class
[self.window addSubview:lCustomPopUpView];
[self.window bringSubviewToFront:lCustomPopUpView];
mPopupTimer = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:#selector(closePopUp) userInfo:Nil repeats:FALSE];
}
- (void)closePopUp{
if (mPopupTimer != nil) {
[mPopupTimer invalidate];
mPopupTimer = nil;
}
for (UIView *lView in self.window.subviews) {
if ([lView isKindOfClass:[CustomPopUpView class]]) {
[lView removeFromSuperview];
}
}
}
Its better to have view behind the UIImageView to handle the TapGesture.
But here is the fix for you code:
Add this to viewDidLoad method to setup you image view to handle tap gesture
//By default the UserInteraction is disabled in UIImageView
[self.testImageView setUserInteractionEnabled:YES];
UITapGestureRecognizer *tapgesture=[[UITapGestureRecognizer alloc]initWithTarget:self action:#selector(ShowImage:)];
tapgesture.numberOfTapsRequired=1;
tapgesture.numberOfTouchesRequired=1;
[self.testImageView addGestureRecognizer:tapgesture];
here is the method handle the gesture event
-(void)ShowImage:(UIGestureRecognizer*)recognizer{
recognizer.view.alpha=0.0;
((UIImageView*)recognizer.view).image = [UIImage imageNamed:#"clone.jpg"];
[UIView animateWithDuration:1.0 delay:0 options:0 animations:^{
recognizer.view.alpha=1.0;
} completion:^(BOOL finished) {
//[self performSelector:#selector(hideImage:) withObject:recognizer.view afterDelay:10];
[UIView animateWithDuration:1.0 delay:3.0 options:0 animations:^{
recognizer.view.alpha=0.0;
} completion:^(BOOL finished) {
((UIImageView*)recognizer.view).image=nil;
((UIImageView*)recognizer.view).alpha=1.0;
}];
}];
}
If you set the alpha of view to 0, then your view will not receive any touch event further. So its best practice to set it again to 1.0 after removed the image from UIImageView.
You may want to consider using a UIButton. Detecting touches with these is easy - as is changing their image.
You could also subclass UIControl (see http://www.raywenderlich.com/36288/how-to-make-a-custom-control).
my goal is to create a simple popover view over a bar button in my tool bar. So apparently Apple doesn't want me to create this on my iPhone. But i already saw something familiar on other apps.
So instead of using the UIPopOver thing from Apple, i like to create an UIView which appears when the bar button is clicked. And disappears when it is clicked again. It would be over the top if i also could create the little arrow which points to the button (you know which arrow i mean :D). But this is not relevant at this time. I just want to create this "popover appears and disappears with the same button". My "solution" right know is not a solution:
- (IBAction)buttonPressed:(id)sender
{
UIView *myPopOverView = [[UIView alloc] initWithFrame:CGRectMake(25, 25, 500, 250)];
myPopOverView.alpha = 0.0;
myPopOverView.layer.cornerRadius = 5;
myPopOverView.layer.borderWidth = 1.5f;
myPopOverView.layer.masksToBounds = YES;
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self
action:#selector(testNSLog)
forControlEvents:UIControlEventTouchUpInside];
[button setTitle:#"Show myPopOverView" forState:UIControlStateNormal];
button.frame = CGRectMake(10, 10, 100, 30);
[myPopOverView addSubview:button];
[self.view addSubview:myPopOverView];
[UIView animateWithDuration:0.4 animations:^{
[myPopOverView setAlpha:1.0];
} completion:^(BOOL finished) {}];
}
So i don't have a clue on how to do that with this code fragment. It appears right away as i want. But i don't know how to create this toggle. I tried with a simple Bool-If-Toggle, but it didn't work. And i don't really know how to remove it from my view either.
Can you guys help me with this?
Thanks in advance!
Ok i solved it.
i defined a UIView in the .h-File and instantiate it in ViewDidLoad, then i did:
- (IBAction)buttonPressed:(id)sender
{
if (showedOptions == NO) {
[self.view addSubview:self.popoverView];
[UIView animateWithDuration:0.4 animations:^{
[self.popoverView setAlpha:1.0];
} completion:^(BOOL finished) {}];
showedOptions = YES;
return;
} else {
showedOptions = NO;
[self.popoverView removeFromSuperview];
}
}