Custom button in tabbar problem - iphone

I've a TabBarController Application.
This code in didFinishingLaunching method:
UIImage *buttonImage = [UIImage imageNamed:#"post-button2.png"];
UIButton* button = [UIButton buttonWithType:UIButtonTypeCustom];
NSLog(#"Button size: %f, %f", buttonImage.size.width, buttonImage.size.height);
button.frame = CGRectMake(0.0, 0.0, buttonImage.size.width, buttonImage.size.height);
[button setBackgroundImage:buttonImage forState:UIControlStateNormal];
CGFloat heightDifference = buttonImage.size.height - self.tabBarController.tabBar.frame.size.height;
NSLog(#"self.tabBarController.tabBar.frame.size.height: %f", self.tabBarController.tabBar.frame.size.height);
NSLog(#"heightDifference: %f", heightDifference);
NSLog(#"%Tabbar: %f, %f", tabBarController.tabBar.center.x, tabBarController.tabBar.center.y);
if (heightDifference < 0)
button.center = tabBarController.tabBar.center;
else {
CGPoint center = self.tabBarController.tabBar.center;
center.y = center.y - heightDifference/2.0;
button.center = tabBarController.tabBar.center;
}
NSLog(#"%Button: %f, %f", button.center.y, button.center.x);
[tabBarController.view addSubview:button];
[self.window addSubview:tabBarController.view];
[self.window makeKeyAndVisible];
The output is:
Where is the problem? I can solve with hardcoded code:
CGPoint center = tabBarController.tabBar.center;
center.x = 160.00;
center.y = 455.50;
button.center = center;
But I'm not sure that is correct.
Ty.

To set the image of a tab you need to set the tabBarItem of the viewcontroller associated with the tab.
See UITabBarItem ref for how to create one with custom image.
So you create a UITabBarItem object with the custom image and then make it the tabBarItem property of the viewcontroller for the tab.
The UITabBarController will then manage putting the image in the right place.

Related

iphone vs ipad simulator image size not behaving as it should

buttonTapped method check if it is an small iphone, normal iphone or ipad. Set the image name and size accordingly. There is no problem with it, works as expected.
When clicked on the "pressedClassicButton" it reloads some data in other views and changes images of some other uiviews. it should not touch imageView in any way. The data it changes are not connected to imageView also.
But somehow when i call the "pressedClassicButton" method, it starts ignoring my specified ipad frame sizes and sets the imageView size as specified on the storyboard constraints. This problem only happens in ipad.
The constraints are: 1:1 ratio, 20px to left right and top.
- (IBAction)pressedClassicButton:(id)sender {
[Flurry logEvent:#"Pressed->SelectPage->Classic"];
[self toggleButton:sender];
self.currentArray = self.classicArray;
[self.swipeView reloadData];
}
-(void)toggleButton:(UIButton *)button{
UIImage *inactiveButton = [UIImage imageNamed:#"fruitify_tab_inactive.png"];
UIImage *activeButton = [UIImage imageNamed:#"fruitify_tab_active.png"];
[self.buttonClassicOut setBackgroundImage:inactiveButton forState:UIControlStateNormal];
[self.buttonGroupOut setBackgroundImage:inactiveButton forState:UIControlStateNormal];
[self.buttonSpecialOut setBackgroundImage:inactiveButton forState:UIControlStateNormal];
[self.buttonTropicalOut setBackgroundImage:inactiveButton forState:UIControlStateNormal];
[button setBackgroundImage:activeButton forState:UIControlStateNormal];
}
- (void)buttonTapped:(UIButton *)sender
{
int clicked = (int)sender.tag;
self.lastChoice = clicked;
NSString *str = self.currentArray[clicked][3];
for (UIView *subView in self.imageView.subviews)
{
if (subView.tag < 120 && subView.tag > 100)
{
[subView removeFromSuperview];
}
}
NSString *machineName = [[NSString alloc] initWithString:[MasterClass MachineName]];
if ([machineName rangeOfString:#"iPad"].location != NSNotFound) {
NSLog(#"ipad");
self.imageView.frame = CGRectMake(120.0, 80.0, 520, 520);
} else if ([machineName rangeOfString:#"iPhone3"].location != NSNotFound) {
self.imageView.frame = CGRectMake(50.0, 70.0, 220, 220);
NSLog(#"iphone3-4");
} else if ([machineName rangeOfString:#"iPod"].location != NSNotFound) {
self.imageView.frame = CGRectMake(50.0, 70.0, 220, 220);;
NSLog(#"iphone3-4");
}
else{
self.imageView.frame = CGRectMake(20.0, 82.0, 280, 280);
NSLog(#"iphone5+");
}
self.imageView.image = [UIImage imageNamed:self.currentArray[clicked][4]];
[self.imageView setUserInteractionEnabled:YES];
float realImageSize = self.imageView.image.size.height;
float viewBoundSize = self.imageView.bounds.size.height;
float sizeDifference = realImageSize / viewBoundSize;
if (![str isEqual: #"group"]) {
CGPoint eye = CGPointMake([self.currentArray[clicked][8] floatValue] / sizeDifference, [self.currentArray[clicked][9] floatValue] / sizeDifference);
CGPoint mouth = CGPointMake([self.currentArray[clicked][10] floatValue] / sizeDifference, [self.currentArray[clicked][11] floatValue] / sizeDifference);
CGPoint center = CGPointMake( (eye.x + mouth.x)/2 , (eye.y + mouth.y)/2 );
CGFloat xDist = (eye.x - mouth.x);
CGFloat yDist = (eye.y - mouth.y);
CGFloat faceSize = sqrt((xDist * xDist) + (yDist * yDist));
UIImage *tapFace = [UIImage imageNamed:#"fruitify_face_button.png"];
CGRect newBound = CGRectMake(center.x - faceSize, center.y - faceSize, faceSize*2, faceSize*2);
UIButton *tapButton = (UIButton *)self.imageView;
tapButton = [UIButton buttonWithType:UIButtonTypeCustom];
[tapButton setFrame:newBound];
[tapButton setBackgroundImage:tapFace forState:UIControlStateNormal];
tapButton.userInteractionEnabled = YES;
tapButton.tag = 113;
tapButton.enabled = YES;
[tapButton addTarget:self action:#selector(tapHereTapped:) forControlEvents:UIControlEventTouchUpInside];
[self.imageView addSubview:tapButton];
}else if([str isEqual:#"group"]){
int faceCount = [self.currentArray[clicked][8] count];
for (int i = 0; i < faceCount; i++) {
CGPoint eye = CGPointMake([self.currentArray[clicked][8][i] floatValue] / sizeDifference, [self.currentArray[clicked][9][i] floatValue] / sizeDifference);
CGPoint mouth = CGPointMake([self.currentArray[clicked][10][i] floatValue] / sizeDifference, [self.currentArray[clicked][11][i] floatValue] / sizeDifference);
CGPoint center = CGPointMake( (eye.x + mouth.x)/2 , (eye.y + mouth.y)/2 );
CGFloat xDist = (eye.x - mouth.x);
CGFloat yDist = (eye.y - mouth.y);
CGFloat faceSize = sqrt((xDist * xDist) + (yDist * yDist));
UIImage *tapFace = [UIImage imageNamed:#"fruitify_face_button.png"];
CGRect newBound = CGRectMake(center.x - faceSize, center.y - faceSize, faceSize*2, faceSize*2);
UIButton *tapButton = (UIButton *)self.imageView;
tapButton = [UIButton buttonWithType:UIButtonTypeCustom];
[tapButton setFrame:newBound];
[tapButton setBackgroundImage:tapFace forState:UIControlStateNormal];
tapButton.userInteractionEnabled = YES;
tapButton.tag = 113 + i;
tapButton.enabled = YES;
[tapButton addTarget:self action:#selector(tapHereTapped:) forControlEvents:UIControlEventTouchUpInside];
[self.imageView addSubview:tapButton];
}
}
}
Unexpected things happen when you use constraints and try to set frames as well. Try setting the sizes of your views by altering their constraints instead. You add them as properties from your storyboard by ctrl click drag like you do when hooking up a button.

Postioning UIImageSubview at center in UIScrollview

How to position the UIImageSubview at center in UIScrollview , I am Loading images dynalically in the uiscrollview so when i select any image in the Uiscrollview that Image should display at the center of screen in uiscrollview.
My Image Width is 65 and Screen width is 320 , I am facing problem setting the position of choosen image at the center of screen,
[scrollView1 setContentOffset:CGPointMake(130, 0) animated:YES];
scrollview just scroll to the 130 position. I don't want like this , I want the subview should be displayed at the center.
Please see the screenshot and tell me how to solve this problem.
How about
(pseudocode)
contentOffset.x = image.center.x - (screenwidth/2)
(expanded...)
UIImageView* imageView = //imageview that was selected
CGFloat screenWidth = 320;
CGPoint offset = scrollView1.contentOffset;
offset.x = imageView.center.x - screenWidth/2;
[scrollView1 setContentOffset:offset animated:YES];
I assume you already worked out how to get a pointer to the selected image...
(expanded further...)
Here is a fully-worked out version of the same, using buttons instead of images for ease of selection... try it (just copy into a new single-view project's viewContoller]), you will see that it works. If you are still having problems I expect it is the way you are identifying your image as the selected image for centering. You would need to update your question showing your code...
- (void)viewDidLoad
{
[super viewDidLoad];
CGRect frame = self.view.bounds;
frame.size.height = frame.size.height/2;
self.scrollView = [[UIScrollView alloc] initWithFrame:frame];
[self.view addSubview:self.scrollView];
[self.scrollView setContentSize:CGSizeMake(2000,self.scrollView.bounds.size.height)];
[self.scrollView setDelegate:self];
for (int i = 1; i < 20; i++) {
UIButton* button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
NSString* title = [NSString stringWithFormat:#"button %d",i];
[button setTitle:title forState:UIControlStateNormal];
button.bounds = CGRectMake(0,0,80,60);
button.center = CGPointMake(i*100+50,self.scrollView.bounds.size.height/2);
[button addTarget:self
action:#selector(buttonPressed:)
forControlEvents:UIControlEventTouchUpInside];
[self.scrollView addSubview:button];
}
}
- (void)buttonPressed:(UIButton*)sender
{
CGFloat x = sender.center.x - self.scrollView.bounds.size.width/2.0f;
[self.scrollView setContentOffset:CGPointMake(x,0) animated:YES];
}

Modify Height of Single Tab in UITabBar

Is there a way so that I can increase the height of a single tab (UITabbarItem) keeping the rest of the tabs with same height in UITabBar.
I am just taking a guess that you are trying to have an instagram (previous instagram) or keek like interface where one of the tabBarItems were much larger than the rest.
You have two options.
Create a customTabBar with one of the items much larger. (I will not recommend this)
Just keep the existing tabBarItems as they are. There is no need for a custom one.
You will just have to create a UIButton with the required height and shape and add that on top of your tabBarItem.
Our work then comes down to creating a subclass of UITabBarController and add a custom UIButton on top of the UITabBar.
UIButton* button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0.0, 0.0, buttonImage.size.width, buttonImage.size.height);
[button setBackgroundImage:buttonImage forState:UIControlStateNormal];
[button setBackgroundImage:highlightImage forState:UIControlStateHighlighted];
CGFloat heightDifference = buttonImage.size.height - self.tabBar.frame.size.height;
if (heightDifference < 0)
button.center = self.tabBar.center;
else
{
CGPoint center = self.tabBar.center;
center.y = center.y - heightDifference/2.0;
button.center = center;
}
[self.view addSubview:button];
From my experience, you must extend the UITabBar class and when you initialize your UITabBarItems, set their heights accordingly. Also, create a background image for your UITabBar.
You can use customTabBar, here you can get sample of it, its nice classes and you can set tabBar item according to your size.
Link
In Swift 2.0, you can have a subclass of UITabBarController and add the UIButton using the following code:
let button: UIButton = UIButton(type: UIButtonType.Custom)
button.backgroundColor = UIColor.clearColor()
let buttonImage: UIImage = UIImage(named: "logo")!
let buttonImageHighlighted: UIImage = UIImage(named: "logoHighlighted")!
button.frame = CGRectMake(0.0, 0.0, buttonImage.size.width, buttonImage.size.height);
button.setBackgroundImage(buttonImage, forState: UIControlState.Normal)
button.setBackgroundImage(buttonImageHighlighted, forState: UIControlState.Highlighted)
let heightDifference:CGFloat = buttonImage.size.height - self.tabBar.frame.size.height;
if (heightDifference < 0) {
button.center = self.tabBar.center;
} else {
var center: CGPoint = self.tabBar.center;
center.y = center.y - heightDifference/2.0;
button.center = center;
}
self.view.addSubview(button)

Identifying the Position of a UIButton

I've created 5 buttons dynamically, as follows:
float xpos=0,ypos=0;
for (int i = 0; i < 5; i++)
{
but = [UIButton buttonWithType:UIButtonTypeCustom];
[but setTag:i];
[but setImage:[UIImage imageNamed:#"btfrnt.png"] forState:UIControlStateNormal];
[but setFrame:CGRectMake(xpos, ypos, 40, 40)];
xpos+=80;
[but addTarget:self action:#selector(checkboxButton:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:but];
}
In Click event of any of the button, I want to find the position of the button which I have clicked....
-(void)checkboxButton:(UIButton*)sender
{
NSLog(#"%d",xpos);
}
But, it displays only the xpos of the last button...Is there any way to identify it by means of its tag?
try this :
-(void)checkboxButton:(UIButton*)sender
{
NSLog(#"%f %f",sender.frame.origin.x,sender.frame.origin.y);
}
I take it xPos is a global variable - of course it contains the value of the last button, that's the value it was set to last.
For the button position you don't need to store anything in a global variable - just get it from the sender object that is delivered to you in the click event handler - it is a UIButton pointer, and the UIButton object has a frame structure with origin.x and origin.y, as well as a size.width and size.height, by the way.
You may try this...
-(void)checkboxButton:(UIButton*)sender {
UIButton *button = (UIButton *)sender;
CGRect rect = button.frame; //You may use sender.frame directly
NSLog(#"X: %d", rect.origin.x);
NSLog(#"Y: %d", rect.origin.y);
NSLog(#"Width: %f", rect.size.width);
NSLog(#"Height: %f", rect.size.height);
}

how to resize the button image in iphone

im new in iphone.now im developing button image view in iphone.here images are images displaying large size i want to resize the images. that means images are displayed like medium size. i implement code like this.
images = [[NSMutableArray alloc]init];
[images addObject:[UIImage imageNamed:#"bearlarge.jpg"]];
[images addObject:[UIImage imageNamed:#"bufflo_large.jpg"]];
[images addObject:[UIImage imageNamed:#"camel_large.jpg"]];
UIView *view = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
int row = 0;
int column = 0;
for(int i = 0; i < images.count; ++i) {
// UIImage *thumb = [images objectAtIndex:i];
UIButton * button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(column*105+14, row*105+10, 50,50);
[button setImage:[images objectAtIndex:i] forState:UIControlStateNormal];
[button addTarget:self
action:#selector(buttonClicked:)
forControlEvents:UIControlEventTouchUpInside];
button.tag = i;
[view addSubview:button];
if (column == 2) {
column = 0;
row++;
} else {
column++;
}
}
//[view setContentSize:CGSizeMake(320, (row+1) * 80 + 10)];
self.view = view;
[view release];
can any one plz send me code for how to resize the button images in iphone.
Thank you in advançe.
You could create this category for UIImage:
- (UIImage *)rescaleImageToSize:(CGSize)size {
CGRect rect = CGRectMake(0.0, 0.0, size.width, size.height);
UIGraphicsBeginImageContext(rect.size);
[self drawInRect:rect]; // scales image to rect
UIImage *resImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return resImage;
}
Better add a UIImageView over the button. So that you can easily change the frame size.
CODE
UIButton *imageBut=[[UIButton alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
UIImageView *img=[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
img.userInteractionEnabled=NO;
[imageBut addSubview:img];
[self.view addSubview:imageBut];
[imageBut release];
[img release];
Or else, use
[button setBackgroundImage:YourImage];
setImage will set the image with original size in which you are included that into your project.
setBackgroundImage will resize the original image.
[button setBackgroundImage:[images objectAtIndex:i] forState:UIControlStateNormal];
-(void)scaleImage:(id)sender
{
if([(UIPinchGestureRecognizer*)sender state] == UIGestureRecognizerStateEnded)
{
lastScale = 1.0;
return;
}
CGFloat scale = 1.0 - (lastScale - [(UIPinchGestureRecognizer*)sender scale]);
CGAffineTransform currentTransform = [(UIPinchGestureRecognizer*)sender view].transform;
CGAffineTransform newTransform = CGAffineTransformScale(currentTransform, scale, scale);
[[(UIPinchGestureRecognizer*)sender view] setTransform:newTransform];
lastScale = [(UIPinchGestureRecognizer*)sender scale];
}