I am building an app using Xcode 5 and used images for retina iPhone 5 as well launch images both in 640x960 and 640x1136.
When I run the app on iPhone 5 everything works grew.
When I run it on iPhone 4/4s some of my buttons are lost seems like the app is over sized.
What am i doing wrong?
Buttons created programmatically and code is here:
self.questionLabel.text = self.question;
[self.imageView setImage:self.image];
UIImage *normal = [UIImage imageNamed:#"question_button.png"];
UIImage *selected = [UIImage imageNamed:#"question_button_selected.png"];
self.btnOne = [UIButton buttonWithType:UIButtonTypeCustom];
self.btnOne.tag = 1;
[self.btnOne.titleLabel setFont:[UIFont systemFontOfSize:15.0f]];
[self.btnOne setBackgroundImage:normal forState:UIControlStateNormal];
[self.btnOne setBackgroundImage:selected forState:UIControlStateHighlighted];
self.btnTwo = [UIButton buttonWithType:UIButtonTypeCustom];
self.btnTwo.tag = 2;
[self.btnTwo.titleLabel setFont:[UIFont systemFontOfSize:15.0f]];
[self.btnTwo setBackgroundImage:normal forState:UIControlStateNormal];
[self.btnTwo setBackgroundImage:selected forState:UIControlStateHighlighted];
self.btnThree = [UIButton buttonWithType:UIButtonTypeCustom];
self.btnThree.tag = 3;
[self.btnThree.titleLabel setFont:[UIFont systemFontOfSize:15.0f]];
[self.btnThree setBackgroundImage:normal forState:UIControlStateNormal];
[self.btnThree setBackgroundImage:selected forState:UIControlStateHighlighted];
self.btnFour = [UIButton buttonWithType:UIButtonTypeCustom];
self.btnFour.tag = 4;
[self.btnFour.titleLabel setFont:[UIFont systemFontOfSize:15.0f]];
[self.btnFour setBackgroundImage:normal forState:UIControlStateNormal];
[self.btnFour setBackgroundImage:selected forState:UIControlStateHighlighted];
//Assign the frames
_btnOne.frame = [((NSValue *)[indexArray objectAtIndex:0]) CGRectValue];
_btnTwo.frame = [((NSValue *)[indexArray objectAtIndex:1]) CGRectValue];
_btnThree.frame = [((NSValue *)[indexArray objectAtIndex:2]) CGRectValue];
_btnFour.frame = [((NSValue *)[indexArray objectAtIndex:3]) CGRectValue];
Related
I have a large amount of image buttons being added to the screen in an iPhone app. Everything is working fine with my code.
The problem is the subviews are not displaying until every one of the subviews are ready to display. So for example I have 48 subviews, I don't want to wait until every subview is ready, I want to visibly be able to see each one appearing on the screen as they are loading.
I hope this makes sense. The issue is getting larger as i add more images to the collection.
This is my code for you to see:
for (int i = 0; i < imgNumbers.count; i++) {
myButton = [UIButton buttonWithType:UIButtonTypeCustom];
CGRect buttonValue = [[btn9Locations objectAtIndex:location] CGRectValue];
myButton.frame = buttonValue;
NSString *imagePart1 = [[NSString alloc] initWithFormat:#"%#", imgNumbers[i]];
[myButton addTarget:self action:#selector(buttonClick:) forControlEvents:UIControlEventTouchDownRepeat];
NSURL *tmpDirURL = [NSURL fileURLWithPath:NSTemporaryDirectory() isDirectory:YES];
NSURL *fileURL = [tmpDirURL URLByAppendingPathComponent:imagePart1];
NSString *imageLocation = [[NSString alloc]initWithFormat:#"%#", [fileURL path]];
UIImage * image = [UIImage imageWithContentsOfFile:imageLocation];
[myButton setBackgroundColor:[UIColor colorWithRed:0.94 green:0.39 blue:0.13 alpha:0.0]];
[myButton setBackgroundImage:image forState:UIControlStateNormal];
[myButton setTitle:[imageNames[i] uppercaseString] forState:UIControlStateNormal];
[myButton.titleLabel setFont:[UIFont fontWithName:#"Helvetica-Light" size:20]];
[myButton setContentHorizontalAlignment:UIControlContentHorizontalAlignmentRight];
[myButton setContentVerticalAlignment:UIControlContentVerticalAlignmentBottom];
[myButton setTitleColor:[UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:0.5] forState:UIControlStateNormal];
myButton.imageView.layer.cornerRadius = 7.0f;
myButton.layer.shadowRadius = 3.0f;
myButton.layer.shadowColor = [UIColor blackColor].CGColor;
myButton.layer.shadowOffset = CGSizeMake(0.0f, 1.0f);
myButton.layer.shadowOpacity = 0.5f;
myButton.layer.masksToBounds = NO;
[buttons addObject:myButton];
[scrollview addSubview:myButton]; }
Hope this makes sense, I have seen apps that when you scroll down each item fades onto the screen but I don't seem to be able to sort this part first!
Thanks
Try to move initialisation code into background queue and only addSubview call on main queue.
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
for (int i = 0; i < imgNumbers.count; i++) {
myButton = [UIButton buttonWithType:UIButtonTypeCustom];
CGRect buttonValue = [[btn9Locations objectAtIndex:location] CGRectValue];
myButton.frame = buttonValue;
NSString *imagePart1 = [[NSString alloc] initWithFormat:#"%#", imgNumbers[i]];
[myButton addTarget:self action:#selector(buttonClick:) forControlEvents:UIControlEventTouchDownRepeat];
NSURL *tmpDirURL = [NSURL fileURLWithPath:NSTemporaryDirectory() isDirectory:YES];
NSURL *fileURL = [tmpDirURL URLByAppendingPathComponent:imagePart1];
NSString *imageLocation = [[NSString alloc]initWithFormat:#"%#", [fileURL path]];
UIImage * image = [UIImage imageWithContentsOfFile:imageLocation];
[myButton setBackgroundColor:[UIColor colorWithRed:0.94 green:0.39 blue:0.13 alpha:0.0]];
[myButton setBackgroundImage:image forState:UIControlStateNormal];
[myButton setTitle:[imageNames[i] uppercaseString] forState:UIControlStateNormal];
[myButton.titleLabel setFont:[UIFont fontWithName:#"Helvetica-Light" size:20]];
[myButton setContentHorizontalAlignment:UIControlContentHorizontalAlignmentRight];
[myButton setContentVerticalAlignment:UIControlContentVerticalAlignmentBottom];
[myButton setTitleColor:[UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:0.5] forState:UIControlStateNormal];
myButton.imageView.layer.cornerRadius = 7.0f;
myButton.layer.shadowRadius = 3.0f;
myButton.layer.shadowColor = [UIColor blackColor].CGColor;
myButton.layer.shadowOffset = CGSizeMake(0.0f, 1.0f);
myButton.layer.shadowOpacity = 0.5f;
myButton.layer.masksToBounds = NO;
[buttons addObject:myButton];
dispatch_async(dispatch_get_main_queue(), ^{
[scrollview addSubview:myButton];
});
}
});
I had an application in which i am setting the content size of the scroll view dynamically according to the number of buttons which i am creating from an array.like this `
for(int i =0;i<[sarray count];i++)
{
NSMutableDictionary *dicttable=[sarray objectAtIndex:i];
NSString *head=[dicttable objectForKey:#"cat"];
btn= [UIButton buttonWithType:UIButtonTypeCustom];
int j=i+1;
btn.frame = CGRectMake((j-1)*87,0,87, 44);
[btn setBackgroundImage:[UIImage imageNamed:#"bar.png"] forState:UIControlStateNormal];
[btn setBackgroundImage:[UIImage imageNamed:#"bar_hvr.png"] forState:UIControlStateSelected];
btn.backgroundColor = [UIColor clearColor];
btn.titleLabel.font = [UIFont fontWithName:#"Helvetica-Bold" size:14];
btn.titleLabel.textColor = [UIColor whiteColor];
[btn setTitle:head forState:UIControlStateNormal];
btn.tag = i;
[btn setShowsTouchWhenHighlighted:YES];
[Scroller addSubview:btn];
[btn addTarget:self action:#selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
if(btn.tag==0)
{
sendActionsForControlEvents:UIControlEventTouchUpInside];
}
else
{
btn.selected=NO;
}
}
}
[Scroller setContentSize:CGSizeMake([sarray count]*85, 44)];
`
but here the problem is after the last button also the scroll view scrolls .it need not to be happend,i need the scrolling ends with last button on the view,can anybody help me on this
set scrollview bounces to NO
scrollView.bounces = NO;
yes as you said it looses the smoothness. But no other choice. If you found any sol please update the answer.
hey i am sending you complete example to make button dynamicay and seting the content size scrollview on number of count ,i had just given u one example code u have to set the size of frame of button as u requried
UIScrollView *scroll_View=[[UIScrollView alloc]initWithFrame:self.view.bounds];
[self.view addSubview:scroll_View];
int y=0;
for(int i =1;i<10;i++)
{
// NSMutableDictionary *dicttable=[sarray objectAtIndex:i];
// NSString *head=[dicttable objectForKey:#"cat"];
UIButton* btn= [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn.frame = CGRectMake(y,89,100, 50);
[btn setBackgroundImage:[UIImage imageNamed:#"bar.png"] forState:UIControlStateNormal];
[btn setBackgroundImage:[UIImage imageNamed:#"bar_hvr.png"] forState:UIControlStateSelected];
btn.backgroundColor = [UIColor clearColor];
btn.titleLabel.font = [UIFont fontWithName:#"Helvetica-Bold" size:14];
NSString *str=[NSString stringWithFormat:#"%d",i];
btn.titleLabel.textColor = [UIColor whiteColor];
[btn setTitle:str forState:UIControlStateNormal];
btn.tag = i;
[btn setShowsTouchWhenHighlighted:YES];
[scroll_View addSubview:btn];
[btn addTarget:self action:#selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
y=110*i;
}
[scroll_View setContentSize:CGSizeMake(y, 400)];
enjoy coding
You just declare one yMargin variable of type CGFloat and initialize it with some required value as 0. Now every time whenever you are adding a new subview vertically do y+=lbl.frame.size.height;
Now setContentSize:CGSizeMake([sarray count]*85, y margin+someRequiredMargin)];
(someRequiredMargin - you want to add below )
I am developing application like Instagram.
I have code for cellForRowAtIndexPath, every time it creates object for unbutton and it is autorelease. this button display username and it may be 1,2,3,4,5,.. total like users. there for i create UIButton on depend on like cout.
I am not using ARC.
but it crash sometimes (Not everyTime) when setColour.
I enabled NSZombie and it keeps saying:
[UIDeviceRGBColor set]: message sent to deallocated instance 0x21b526f0
UIButton *btnLikeUserName = [[[UIButton alloc] init] autorelease];
[btnLikeUserName.titleLabel setFont:[UIFont boldSystemFontOfSize:12]];
if (posx + size.width > 280) {
posy = posy + 22;
posx = 18;
btnLikeUserName.frame = CGRectMake(posx,posy,size.width,size.height);
posx = posx + size.width + 5;
}
else {
btnLikeUserName.frame = CGRectMake(posx,posy,size.width,size.height);
posx = posx + size.width + 5;
}
btnLikeUserName.tag = shareObjU.userId;
[btnLikeUserName setTitle:text forState:UIControlStateNormal];
[btnLikeUserName setTitleColor:[UIColor colorWithRed:50/255.0 green:79/255.0 blue:133/255.0 alpha:1.0] forState:UIControlStateNormal]; ////Hear is crash
[btnLikeUserName addTarget:self action:#selector(btnLikeUserNameClick:) forControlEvents:UIControlEventTouchUpInside];
[cell.viewLikeComment addSubview:btnLikeUserName];
i also see following link
UISwitch setThumbTintColor causing crash (iOS 6 only)?
1 out of 4 colors return errors
iOS App crashes when using colorWithRed:green:blue:alpha
and other .. but all says for global Objct. not local object. how can i solved it.
Thank You
try this code...
[btnLikeUserName setTitleColor:[UIColor colorWithRed:((float) 50 / 255.0f)
green:((float) 79 / 255.0f)
blue:((float) 133 / 255.0f)
alpha:1.0f] forState:UIControlStateNormal];
OR Also you can set UIColor like bellow.
[btnLikeUserName setTitleColor:[UIColor colorWithRed:50.0f/255.0f green:79.0f/255.0f blue:133.0f/255.0f alpha:1.0] forState:UIControlStateNormal];
And Here you Add UIButtons in every cell then try to define like bellow not alloc and autorelease everytime .....
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
*UPDATE:*See the example which i add in my every cell with multiple button like GridView
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = rect;
button.tag = imageIndex;
[button addTarget:self action:#selector(btnTemp_Clicked:)
forControlEvents:UIControlEventTouchDown];
[button setTitle:[arrTime objectAtIndex:imageIndex] forState:UIControlStateNormal];
[button.titleLabel setFont:[UIFont fontWithName:#"Helvetica-Bold" size:12]];
//[button setTitleColor:[UIColor colorWithPatternImage:[UIImage imageNamed:#"circle03.jpg"]] forState:UIControlStateNormal ];
[button setTitleColor:[UIColor colorWithRed:50.0f/255.0f green:79.0f/255.0f blue:133.0f/255.0f alpha:1.0] forState:UIControlStateHighlighted ];
[button setNeedsDisplay];
[gridCell.contentView addSubview:button];
I want to display two buttons inside the popoverview. I am using WEPOPOVER, I am able to display one btn.
But I am not sure how to display the two buttons.
UIButton *editBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[editBtn setTitle:#"EDIT ME" forState:UIControlStateNormal];
[editBtn setFrame:CGRectMake(250, 0, 100,40)];
[editBtn addTarget:self action:#selector() forControlEvents:UIControlEventTouchUpInside];
[editBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
editBtn.titleLabel.shadowOffset = CGSizeMake(0, -1);
editBtn.titleLabel.font = [UIFont boldSystemFontOfSize:20];
editBtn.backgroundColor = [UIColor blackColor];
UIButton *deleteBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[deleteBtn setTitle:#"Delete ME" forState:UIControlStateNormal];
[deleteBtn setFrame:CGRectMake(250, 0, 100,40)];
[deleteBtn addTarget:self action:#selector(doSubscription) forControlEvents:UIControlEventTouchUpInside];
[deleteBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
deleteBtn.titleLabel.shadowOffset = CGSizeMake(0, -1);
deleteBtn.titleLabel.font = [UIFont boldSystemFontOfSize:20];
deleteBtn.backgroundColor = [UIColor blackColor];
// place inside a temporary view controller and add to popover
UIViewController *viewCon = [[UIViewController alloc] init];
viewCon.view = (UIView *)[NSArray arrayWithObjects:editBtn,deleteBtn, nil];
viewCon.contentSizeForViewInPopover = editBtn.frame.size; // Set the content size
navPopover = [[WEPopoverController alloc] initWithContentViewController:viewCon];
[navPopover presentPopoverFromRect:editButton.frame
inView:self.view
permittedArrowDirections:UIPopoverArrowDirectionUp | UIPopoverArrowDirectionDown
animated:YES];
You have given the same frame for both buttons. So it shows just one button.
[editBtn setFrame:CGRectMake(250, 0, 100,40)];
[deleteBtn setFrame:CGRectMake(250, 0, 100,40)];
I was wondering if there was any way that I could change the size of the image and also its position within the UIButton?
Thanks
UIButton *playButton = [[UIButton buttonWithType:UIButtonTypeRoundedRect] retain];
playButton.frame = CGRectMake(110.0, 360.0, 100.0, 30.0);
[playButton setTitle:#"Play" forState:UIControlStateNormal];
playButton.backgroundColor = [UIColor clearColor];
[playButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal ];
UIImage *buttonImageNormal = [UIImage imageNamed:#"blueButton.png"];
UIImage *strechableButtonImageNormal = [buttonImageNormal stretchableImageWithLeftCapWidth:12 topCapHeight:0];
[playButton setBackgroundImage:strechableButtonImageNormal forState:UIControlStateNormal];
UIImage *buttonImagePressed = [UIImage imageNamed:#"whiteButton.png"];
UIImage *strechableButtonImagePressed = [buttonImagePressed stretchableImageWithLeftCapWidth:12 topCapHeight:0];
[playButton setBackgroundImage:strechableButtonImagePressed forState:UIControlStateHighlighted];
[playButton addTarget:self action:#selector(playAction:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:playButton];
Source: http://jainmarket.blogspot.com/2009/04/create-uibuttonbutton-with-images.html