Wrong UIImageView dimensions on navigationItem - ios

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

Related

iOS: How to make navigationBar titleView align left

UIView *sLineBarView = [[UIView alloc]initWithFrame:CGRectMake(0,0,1,20)];
[sCustomTitleView addSubview:sLineBarView];
[sLineBarView TUMMoveToVerticalCenter];
[sLineBarView release];
UIImage *sDropBoxImg= [UIImage imageNamed:#"bt_dropdown"];
UIImageView *sDropDownBox = [[UIImageView alloc] initWithImage: sDropBoxImg];
[sDropDownBox setFrame: CGRectMake(0,30, 92, 58)];
[sDropDownBox setAlpha:1.0f];
sDropDownBox.userInteractionEnabled = YES;
sDropDownBox.tag = kDropBoxTag;
[sCustomTitleView addSubview:sDropDownBox];
[sDropDownBox release];
UIImage *sRightImage1 = [UIImage imageNamed:#"gnb_bt_menu"];
UIButton *sCustomBarItem0 = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 20,40)];
[sCustomBarItem0 setBackgroundImage:sRightImage1 forState:UIControlStateNormal];
[sCustomTitleView addSubview:sCustomBarItem0];
[sCustomBarItem0 TUMMoveToVerticalCenter];
sViewController.navigationItem.titleView = sCustomTitleView;
I want to navigationBar titleView alignment left but with no success.
How to make navigationBar titleView align left?
fream.x = negative
In my opinion, this is imprecise.(because ios version)
Increase sCustomTitleView width and check or you can try this code in viewDidLoad-
UILabel* lbl = [[UILabel alloc] initWithFrame:CGRectMake(0,0,200,40)];
lbl.textAlignment = NSTextAlignmentLeft;
lbl.text = #"My Title";
self.navigationItem.titleView = lbl;

didTapInfoWindowOfMarker multiple buttons click event info

I am displaying some map points in google map. when user will click on that map point. I markerInfoWindow will be display. In this window, I am using two buttons. whenever user will click on that buttons. didTapInfoWindowOfMarker fires. but i amn't able to identify that which one has been clicked. may you please help me for that? i don't want to use any other approach.
Thanks
update:- This is infoWindow code.
-(void)prepareBubble
{
// UIView *bubble = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 80)];
self .frame = CGRectMake(0, 0, 260, 130);
self.backgroundColor = [UIColor clearColor];
// self.layer.borderColor = [UIColor grayColor].CGColor;
// self.layer.borderWidth = 1;
// self.layer.cornerRadius = 7;
UIImageView *bg = [[UIImageView alloc] initWithFrame:self.frame];
bg.image = [UIImage imageNamed:#"popup.png"];
[self addSubview:bg];
self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(90, 5, 150, 40)];
self.titleLabel.backgroundColor = [UIColor clearColor];
[self.titleLabel setAdjustsFontSizeToFitWidth:TRUE];
self.titleLabel.numberOfLines = 0;
self.titleLabel.font = [UIFont fontWithName:FontNameArialBold size:14];
self.titleLabel.text = _title;
self.addressLabel= [[UILabel alloc] initWithFrame:CGRectMake(90, 40, 150, 35)];
self.addressLabel.backgroundColor = [UIColor clearColor];
[self.addressLabel setAdjustsFontSizeToFitWidth:TRUE];
self.addressLabel.numberOfLines = 0;
self.addressLabel.font = [UIFont fontWithName:FontNameArial size:12];
self.addressLabel.text = _address;
self.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(15, 15, 70, 70)];
[self addSubview:self.imageView];
self.imageView.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:_imageUrl]]];
if ( self.imageView.image == nil )
{
self.imageView.image = [UIImage imageNamed:DefaultImage100];
}
// [[[CustomNetwork alloc] init] setImage:_imageUrl onImageView:self.imageView withPlaceHolderImage:DefaultImage100];
[self addSubview:self.titleLabel];
[self addSubview:self.addressLabel];
if ( _ratings > 0 )
{
[self addSubview:[self addStar:_ratings]];
}
if( _hasTeeTimes ) {
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setTitle:#"Book" forState:UIControlStateNormal];
[button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
UIImage *buttonImage = [UIImage imageNamed:#"button_orange_large.png"];
[button setBackgroundImage:buttonImage forState:UIControlStateNormal];
button.frame = CGRectMake(200, 75, 45, 25);
[self addSubview:button];
}
// return self;
}
here I am trying to make to click listener, first is on button and another is on tap in any part of this window like as textField etc. so my two new view will push
Change the buttons to properties.
Give the buttons tags.
Logically
determine by identifying the sender and see if it is > or < the other
button (which you need to keep in memory by making them properties or
instance variables).
load custom Xibs, and all of these will work with 0 lines of code.

UIView alpha is disappering after UIButton is clicked

I have a container UIView and a UIView with alpha 0.7f in it, also a custom UIButton with image added to that container UIView. Now whenever I click on the button a big square chunk on transparent UIView disappears and stays that way. I have no idea why this happens. Can anyone help me figure this out?
UIWindow* window = [[UIApplication sharedApplication] keyWindow];
UIView *viewTestOverViewContainer = [[UIView alloc] initWithFrame:window.bounds];
UIView *viewOverlay = [[UIView alloc] initWithFrame:viewTestOverViewContainer.frame];
viewOverlay.backgroundColor = [UIColor blackColor];
viewOverlay.alpha = 0.7;
[viewTestOverViewContainer addSubview:viewOverlay];
float x = (window.bounds.size.width/2)-98;
float y = (window.bounds.size.height/2)-98;
UIView *viewTestPicContainer = [[UIImageView alloc] initWithFrame:CGRectMake(x, y, 204, 204)];
viewTestPicContainer.backgroundColor = [UIColor whiteColor];
viewTestPicContainer.layer.cornerRadius = viewTestPicContainer.frame.size.width / 2;
viewTestPicContainer.clipsToBounds = YES;
viewTestPicContainer.backgroundColor = [UIColor whiteColor];
[viewTestPicContainer setContentMode:UIViewContentModeScaleAspectFill];
UIImageView *imgViewTestPic = [[UIImageView alloc] initWithFrame:CGRectMake(2, 2, 200, 200)];
imgViewTestPic.image = [UIImage imageNamed:#"portrait_eyes_14.jpg"];
imgViewTestPic.layer.cornerRadius = imgViewTestPic.frame.size.width / 2;
imgViewTestPic.clipsToBounds = YES;
imgViewTestPic.backgroundColor = [UIColor whiteColor];
[imgViewTestPic setContentMode:UIViewContentModeScaleAspectFill];
[viewTestPicContainer addSubview:imgViewTestPic];
[viewTestOverViewContainer addSubview:viewTestPicContainer];
UIButton *btnLeftArrow = [UIButton buttonWithType:UIButtonTypeCustom];
btnLeftArrow.frame = CGRectMake(46, 275, 12, 20);
[btnLeftArrow setImage:[UIImage imageNamed:#"popup-left-arrow"] forState:UIControlStateNormal];
[viewTestOverViewContainer addSubview:btnLeftArrow];
UIButton *btnRightArrow = [UIButton buttonWithType:UIButtonTypeCustom];
btnRightArrow.frame = CGRectMake(270, 275, 12, 20);
[btnRightArrow setImage:[UIImage imageNamed:#"popup-right-arrow"] forState:UIControlStateNormal];
[viewTestOverViewContainer addSubview:btnRightArrow];
[window addSubview:viewTestOverViewContainer];
Before:
After clicking the left arrow button:

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

UILongPressGestureRecognizer on a navigationbar's view

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)];

Resources