UILongPressGestureRecognizer on a navigationbar's view - ios

I'm using this code to show a round user's avatar on my navigation bar:
UIView *avatView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 190.0f)];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(-20, 75, 40, 40)];
imageView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
imageView.image = [UIImage imageWithData:myUtente.imgData];
imageView.layer.masksToBounds = YES;
imageView.layer.cornerRadius = 20.0;
imageView.layer.borderColor = [UIColor blueColor].CGColor;
imageView.layer.borderWidth = 1.0f;
imageView.layer.rasterizationScale = [UIScreen mainScreen].scale;
imageView.layer.shouldRasterize = YES;
imageView.clipsToBounds = YES;
[avatView addSubview:imageView];
self.navBar.titleView = avatView;
Now I'd like this avatar to act like a button, with a 2 seconds long press on it, so I've added this code:
UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(switchUser:)];
[longPressGesture setMinimumPressDuration:2.0f];
// same code as above
[avatView addGestureRecognizer:longPressGesture];
The corresponding action never gets called and I don't know why. Any idea?
Thanks.

Change the frame of avatView and imageView then gesture works like a charm.
ex:
UIView *avatView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 44)];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(80, 2, 40, 40)];

Related

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;
}

Animation in content of ScrollView

I want to animate content of scrollview i have tried this for animation.
- (void)viewDidLoad {
[super viewDidLoad];
//Scroll View Created Programmatically
scrollview=[[UIScrollView alloc] initWithFrame:self.view.bounds];
scrollview.delegate=self;
scrollview.contentSize=CGSizeMake(scrollview.bounds.size.width, self.view.bounds.size.height);
[self.view addSubview:scrollview];
// Road Image Created Programmatically
backgroundImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)];
backgroundImageView.image = [UIImage imageNamed:#"roadds.png"];
backgroundImageView.contentMode = UIViewContentModeRedraw;
[scrollview addSubview:backgroundImageView];
// Tree top left Created Programmatically
Tree = [[UIImageView alloc] initWithFrame: CGRectMake(0, 0, 30, 50)];
Tree.image = [UIImage imageNamed:#"rsz_1rsz_tree.png"];
Tree.contentMode = UIViewContentModeTopLeft;
[scrollview addSubview:Tree];
//Tree top Right
Tree1 = [[UIImageView alloc] initWithFrame: CGRectMake(self.view.frame.size.width-30, 0, 30, 50)];
Tree1.image = [UIImage imageNamed:#"rsz_tree1.png"];
Tree1.contentMode = UIViewContentModeTopRight;
[scrollview addSubview:Tree1];
//Tree Bottom left
Tree2 = [[UIImageView alloc] initWithFrame: CGRectMake(0, self.view.frame.size.height-80, 30, 50)];
Tree2.image = [UIImage imageNamed:#"rsz_tree2.png"];
Tree2.contentMode = UIViewContentModeBottomLeft;
[scrollview addSubview:Tree2];
//Tree Bottom Right
Tree3 = [[UIImageView alloc] initWithFrame: CGRectMake(self.view.frame.size.width-30, self.view.frame.size.height-80, 30, 50)];
Tree3.image = [UIImage imageNamed:#"rsz_tree3.png"];
Tree3.contentMode = UIViewContentModeBottomRight;
[scrollview addSubview:Tree3];
// Car
car = [[UIImageView alloc] initWithFrame: CGRectMake((self.view.frame.size.width)/2+20, self.view.frame.size.height-120, 40, 60)];
car.image = [UIImage imageNamed:#"rsz_car1.png"];
car.contentMode = UIViewContentModeCenter;
[scrollview addSubview:car];
//Grass
grass = [[UIImageView alloc] initWithFrame:CGRectMake(0, (self.view.frame.size.height)/2,30, 30)];
grass.image = [UIImage imageNamed:#"rsz_grass.png"];
grass.contentMode = UIViewContentModeBottomLeft;
[scrollview addSubview:grass];
//Grass
grass1 = [[UIImageView alloc] initWithFrame:CGRectMake(self.view.frame.size.width-60, (self.view.frame.size.height)/2,30, 30)];
grass1.image = [UIImage imageNamed:#"rsz_grass1.png"];
grass1.contentMode = UIViewContentModeBottomLeft;
[scrollview addSubview:grass1];
// Left Arrow
left = [[UIButton alloc] initWithFrame:CGRectMake((self.view.frame.size.width)/2+60, (self.view.frame.size.height-35),30, 30)];
[left setBackgroundImage:[UIImage imageNamed:#"rsz_arrowl.png"]forState:UIControlStateNormal];
left.contentMode = UIViewContentModeBottomRight;
[scrollview addSubview:left];
//right arrow
right = [[UIButton alloc] initWithFrame:CGRectMake((self.view.bounds.size.width)/2 - 90, (self.view.frame.size.height-35),30, 30)];
[right setBackgroundImage:[UIImage imageNamed:#"rsz_arrowr.png"]forState:UIControlStateNormal];
right.contentMode = UIViewContentModeBottomLeft;
[scrollview addSubview:right];
// For Animation
CGRect bounds = scrollview.bounds;
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:#"bounds"];
animation.duration = 10;
animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
animation.fromValue = [NSValue valueWithCGRect:bounds];
bounds.origin.y += 100;
animation.toValue =[NSValue valueWithCGRect:bounds];
[scrollview.layer addAnimation:animation forKey:#"bounds"];
scrollview.bounds = bounds;
}
Add UIView to ScrollView and then add your all stuff to this view.
And add animation to this view or view's layer. Hope this will work. :)
If you want your initial position back after animation add this line
bounds.origin.y -= 100;
after
[scrollview.layer addAnimation:animation forKey:#"bounds"];
so, last five line of your viewdidLoad should as below,
bounds.origin.y += 100;
animation.toValue =[NSValue valueWithCGRect:bounds];
[scrollview.layer addAnimation:animation forKey:#"bounds"];
bounds.origin.y -= 100;
scrollview.bounds = bounds;

In my app , when I scroll , the image is going over the battery and signal bar(as shown in the image) which I don't want . How can I make it happen/

The scroll is covering the time and carrier , I just want it not to overlapp but the image view should finish below the carrier bar .
How can this be done.
This is my code for the same
tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320 , self.view.frame.size.height)];
self.tableView.delegate = self;
self.tableView.dataSource = self;
UIImage *image = [UIImage imageNamed:#"dora.jpeg"];
UIGraphicsBeginImageContext(CGSizeMake(320, 480));
[image drawInRect:CGRectMake(0, 0, 320, 480)];
image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
imageView = [[UIImageView alloc] initWithImage:image];
CGRect frame = imageView.frame;
frame.origin.y -= 130;
defaultY = frame.origin.y;
imageView.frame = frame;
self.tableView.backgroundColor = [UIColor clearColor];
UIView *header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 170)];
header.backgroundColor = [UIColor clearColor];
self.tableView.tableHeaderView = header;
[self.view addSubview:imageView];
[self.view addSubview:self.tableView];
defaultSize = CGSizeMake(50, 20);
scrollPanel = [[UIView alloc] initWithFrame:CGRectMake(-defaultSize.width, 0, defaultSize.width, defaultSize.height)];
scrollPanel.backgroundColor = [UIColor blackColor];
scrollPanel.alpha = 0.45;
Thanks
This issue can be very frustrating, and I believe it is a bug on Apple's end, especially because it shows up in their own pre-wired TableViewController from the object library.
For more info refer
iOS 7: UITableView shows under status bar
Set the origin.y of your scrollView (or table) to height of the status bar:
CGRect scrollControllFrame = scrollControll.frame;
scrollControllFrame.origin.y = [UIApplication sharedApplication].statusBarFrame.size.height;
scrollControllFrame.size.height-=[UIApplication sharedApplication].statusBarFrame.size.height;
scrollControll.frame = scrollControllFrame;

How to make a UIImageView a button?

I have a UIImageView like so:
UIImage *image = [UIImage imageNamed:#"test.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
imageView.frame = CGRectMake(0, 0, 30, 30);
imageView.layer.cornerRadius = 5.0;
imageView.layer.masksToBounds = YES;
imageView.layer.borderColor = [UIColor lightGrayColor].CGColor;
imageView.layer.borderWidth = 0.1;
[self.view addSubview:imageView];
Couple of issues/questions:
How do I make this a clickable button?
I want to link to Settings (in my app) but do I have to create that image wheel like the settings icon that so many apps use or is it standard icon in iOS 7?
This is the icon I'm talking about:
Why doesn't my border show up?
1) If you want a button, you should use a UIButton and use [UIButton setImage: forState:]
2) Alternatively, you can add a UITapGestureRecognizer to your current UIImageView
here 2 choice u have to use your method
.h file
#import <QuartzCore/QuartzCore.h> //for radius and corner
1. UIButton
UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeCustom];
[btn1 setTitle:#"Cool title" forState:UIControlStateNormal];
[btn1 setFrame:CGRectMake(7, 7, 150, 160)];
[btn1 setImage:[UIImage imageNamed:#"test.png"] forState:UIControlStateNormal];
[btn1 addTarget:self action:#selector(selectFav) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn1];
-(void) selectFav
{ //what ever you do like
}
2. UITap Gesture for UIImage View
UIImage *image = [UIImage imageNamed:#"test.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
imageView.frame = CGRectMake(0, 0, 30, 30);
imageView.layer.cornerRadius = 5.0;
imageView.layer.masksToBounds = YES;
imageView.layer.borderColor = [UIColor lightGrayColor].CGColor;
imageView.layer.borderWidth = 0.1;
UITapGestureRecognizer *tapGesture1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapGesture:)];
tapGesture1.numberOfTapsRequired = 1;
[tapGesture1 setDelegate:self];
[imgView addGestureRecognizer:tapGesture1];
[self.view addSubview:imageView];
- (void) tapGesture: (id)sender
{
//handle Tap...
}
Try:
UIImage *image = [UIImage imageNamed:#"test.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
imageView.frame = CGRectMake(0, 0, 30, 30);
imageView.layer.cornerRadius = 5.0;
imageView.layer.masksToBounds = YES;
imageView.layer.borderColor = [UIColor lightGrayColor].CGColor;
imageView.layer.borderWidth = 0.1;
/* Here's the button Start */
UIButton *theButton = [[UIButton alloc]initWithFrame:CGRectMake(imageView.frame.origin.x, imageView.frame.origin.y, imageView.frame.size.width, imageView.frame.size.height)];
theButton.backgroundColor = [UIColor clearColor];
theButton.showsTouchWhenHighlighted = YES;
[theButton addTarget:self action:#selector(yourAction) forControlEvents:UIControlEventTouchUpInside];
[imageView addSubview:theButton];
/* Here's the button End */
[self.view addSubview:imageView];
Your border:
0.1 means is 1/10th of a point - try 1.0 for a nice fine line
Also set:
imageView.clipsToBounds = YES;
1:if you have image view and want to clickable
UIImage *image = [UIImage imageNamed:#"test.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
2: Add gesture to image view like this, but don’t forget to setUserInteractionEnabled:YES
UITapGestureRecognizer *TapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(markerMove:)];
[TapGestureRecognizer setNumberOfTapsRequired:1];
[TapGestureRecognizer setNumberOfTouchesRequired:1];
[imageView setUserInteractionEnabled:YES];
[imageView addGestureRecognizer: TapGestureRecognizer];

Gap is existing when customize the navigationbar with imageView

Im using the following codes to customize the navigationbar with an UIView. But why there is a gap in the left size of the navigationbar? ps. the gradient image is test.png.
Thanks in advance!
- (void)viewDidLoad
{
[super viewDidLoad];
// show image
UIImage *image = [UIImage imageNamed: #"test.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage: image];
[imageView setContentMode:UIViewContentModeScaleToFill];
imageView.frame = CGRectMake(0, 0, 320, 44);
// label
UILabel *tmpTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(110, 0, 100, 44)];
tmpTitleLabel.text = #"title";
tmpTitleLabel.textAlignment = UITextAlignmentCenter;
tmpTitleLabel.backgroundColor = [UIColor clearColor];
tmpTitleLabel.textColor = [UIColor whiteColor];
CGRect applicationFrame = CGRectMake(0, 0, 320, 44);
UIView * newView = [[UIView alloc] initWithFrame:applicationFrame];
[newView addSubview:imageView];
[newView addSubview:tmpTitleLabel];
self.navigationItem.titleView = newView;
}
Edit 1
Here is my test code and screen shot
- (void)viewDidLoad
{
[super viewDidLoad];
UIView * viewGreen = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
UIView * viewRed = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
[viewGreen setBackgroundColor:[UIColor greenColor]];
[viewRed setBackgroundColor:[UIColor redColor]];
self.navigationItem.titleView = viewRed;
[self.view addSubview:viewGreen];
}
your code is work fine.just check the image and transparency.
To add the line at last and check again
[self.view addSubview:newView];
problem is solved by the following code
[self.navigationBar insertSubview:imageView atIndex:1];

Resources