i am doing one simple apps project and using storyboards. but i am confuse to use either storyboard or xib file in my project ?
which of the best as performance base.
There is no performance improvements in Storyboard versus Xib files, they both are modified XML files. However storyboards are meant to be easier to use and give better overall look of your app. My personal experience is that storyboards are OK for really small apps(let's say 3-4 screens at top), however if you have complex app with 10-15 screen storyboards becomes a nightmare to maintain.
UPDATE
Also what I don't like about storyboards, is that if I wan't to reuse some screen in other application, I'll have to copy .m and .h files, and then search through storyboard to find my related interface file and put it inside new storyboard. Without them I'll just copy paste h,m,xib files(which I usually put in separate folder) for each screen at new project and use them.
Solution for you last comment :
you can use following code for designing UIButton dynamically-
int yPosition = 0;
for (int i = 0; i<10; i++){
UIButton *aButton = [UIButton buttonWithType:UIButtonTypeCustom];
[aButton setBackgroundColor:[UIColor blackColor]];
[aButton setTitle:[NSString stringWithFormat:#"Button %d",index] forState:UIControlStateNormal];
[aButton setFrame:CGRectMake(20, yPosition, 100, 50)];
[aButton addTarget:self action:#selector(aButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
[aView addSubview:aButton];
yPosition += 50;
}
- (IBAction)aButtonClicked:(id)sender
{
NSLog(#"Clicked on button with title %#",[sender titleLabel].text);
}
Related
I have noticed a that a uibutton is not showing in my app when run in iOS 9 (works fine in earlier OS versions). I know several have noticed a similar issue with storyboards (https://forums.developer.apple.com/thread/14003), though I am not using those and am instead building my view programmatically. There is no autolayout or autoresizingmask to worry about.
UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(SUG_MARGIN, self.mainView.bounds.size.height - 40 - margin, self.mainView.bounds.size.width-margin*2, 40);
[button setTitle:NSLocalizedString(#"...", nil) forState:UIControlStateNormal];
button.titleLabel.font = //...;
button.backgroundColor = //...;
[self.mainView addSubview:button];
NSLog(#"main:%#\nadd:%#\nsuper:%#", NSStringFromCGRect(self.mainView.frame), NSStringFromCGRect(button.frame), button.superview);
That nslog shows everything set up correctly.
I have similar buttons all over the app, but this is the only one that is not appearing, which is why I am scratching my head.
Refactored code to assign only measure frames in viewWillLayoutSubviews and it now works. strange.
I have successfully built the sample application from Vuforia. I am interested in taking the Cylinder Tracking and replacing the soccer ball that rotates around the soda can with a button that does not rotate but sits at a certain position in relation to the can (the center of the tracking image).
I am new to iOS development and am not sure quite how to do this. Ideally this would be done programmatically.
I was able to add a button to the CylinderTargetsViewController.mm using the following code:
- (void)viewDidLoad
{
[super viewDidLoad];
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
[button setTitle:#"Gossamer Rules" forState:UIControlStateNormal];
[button sizeToFit];
button.center = CGPointMake(320/2, 60);
[button addTarget:self action:#selector(buttonPressed:)
forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
}
- (void)buttonPressed:(UIButton *)button {
UIApplication *mySafari = [UIApplication sharedApplication];
NSURL *myURL = [[NSURL alloc]initWithString:#"http://www.example.com"];
[mySafari openURL:myURL];
}
However this adds a button to the screen whether the target is found or not and it has no spatial relationship to the target.
I tried moving this code into CylinderTargetsEAGLView.mm but got an error on line [self.view addSubview:button]; saying view was not a property of self.
I realize this is probably not the right approach. I'm thinking the button needs to be rendered as an openGl object or something so that it is 3 dimensional and can be seen from an angle when moving the target.
Has anyone done this or have any clue how it may be done?
I have decided to use the Vuforia Unity Package as there is a much steeper learning curve trying to learn pure OpenGL programming. The Unity route has much larger community support, and more tutorials / information are available.
I am creating a few buttons programmatically, similar to:
for( int i = 0; i < 5; i++ ) {
UIButton* aButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[aButton setTag:i];
[aButton addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[aView addSubview:aButton];
}
And that I can implement the function of the button this way so that I know witch one was tapped:
- (void)buttonClicked:(UIButton*)button
{
NSLog(#"Button %ld clicked.", (long int)[button tag]);
}
But my question is a little bit trickier than that, I don't want just to access the button inside it's button method but also outside of it so that I can change the UIButton frame using an animation.
I was thinking that I should somehow be able to recreate the pointer to any of the buttons previously created by using the tags that I assign initially.
Can anyone point me in the right direction?
The correct way to do this is by using the next line of code:
UIButton *aButtonReconstruct = (UIButton *)[self.view viewWithTag:aTag];
where aTag is an int and is greater than 0, because all the views have the tag 0 by default, so in the for loop used in the first place, the count should start at minimum 1. In our case aTag having values from 1 to 6 after we change the for loop.
Also there shouldn't be more view with the same tag.
I want to implement tagging functionality like tumblr. I tried with UITextfield and added UIButtons for tags_name in textfield.
How to select tag on first back and delete it on second back?
How tags should be added in multiple lines in UITextfield?
please suggest any solution for this functionality.
here is the code how I added buttons in UItextfield with array.
for (count_value = 1; count_value<=[textfieldarray count]; count_value++)
{
UIButton *button_dish = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button_dish setTag:count_value];
[button_dish.layer setOpacity:0.5];
[button_dish.layer setCornerRadius:4.0f];
[button_dish setUserInteractionEnabled:YES];
[button_dish setShowsTouchWhenHighlighted:YES];
[button_dish setContentMode:UIViewContentModeCenter];
[button_dish setTitleEdgeInsets:UIEdgeInsetsMake(8, 8, 4, 8)];
[button_dish.titleLabel setLineBreakMode:NSLineBreakByWordWrapping];
[button_dish.titleLabel setFont:[UIFont fontWithName:#"Eau-SansBold" size:12]];
[button_dish setContentVerticalAlignment:UIControlContentVerticalAlignmentCenter];
[button_dish setContentHorizontalAlignment:UIControlContentHorizontalAlignmentCenter];
[button_dish setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
[button_dish setContentMode:UIViewContentModeScaleAspectFit];
[button_dish setTitle:textfieldarray[count_value-1] forState:UIControlStateNormal];
CGSize expected_Size1 = [textfieldarray[count_value-1] sizeWithFont:button_dish.titleLabel.font constrainedToSize:button_dish.frame.size lineBreakMode:NSLineBreakByWordWrapping];
[button_dish addTarget:self action:#selector(buttonactionmethod:) forControlEvents:UIControlEventTouchUpInside];
left_view_length1 = left_view_length1+expected_Size1.width+16+12;
[iconview1 setFrame:CGRectMake(0, 10, left_view_length1, 24)];
[button_dish setFrame:CGRectMake(x_cordinate1, 2, expected_Size1.width+16, 20)];
x_cordinate1 = x_cordinate1+expected_Size1.width+16+12;
[iconview1 addSubview:button_dish];
[textfield1 setLeftView:iconview1];
}
Here is the sample reference image for tumblr tag,
1st image shows selection of button on first back.
2nd image shows multiple line tags.
Please give me some reference or hint.
That'd be called a "token field." They first appeared in Mail.app, and eventually became part of the AppKit framework. Documentation here.
It should be pretty easy to reproduce that look in Cocoa Touch. In Cocoa Touch, every view is backed by a Core Animation layer, and layers have a cornerRadius attribute according to your UI. Set that attribute to half the height of the field to create rounded ends. You can set a background color if you're okay with a flat appearance, or you can draw a subtle gradient to give a more convex appearance.
Of course, you only need to do all that if you want a reusable view where you can set the text and so forth. If you only need a few of these for custom buttons or something, it might be easier to just draw them in your favorite drawing program.
Another possibility is to use a third party control, such as any of:
TITokenField
JSTokenField
RMStokenview
COTokenField (part of COPeoplePickerViewController)
Reference from this SO Answer.
i'm trying to create a photo viewer like the Apple Photos app in iOS.
The layout is ok, but it receives memory warning and then crashes. Why? This happens even i load 7/8 images from the app documents folder. Have i to manage the memory with specific system? I use ARC with iOS 5.
EDIT :
The code :
for (int i=0; i<[dataSource count]; i++) {
UIButton *button=[UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:[dataSource objectAtIndex:i] forState:UIControlStateNormal];
[[button titleLabel] setText:[NSString stringWithFormat:#"%i",i+1]];
[button addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
[[button layer] setBorderWidth:1];
[[button layer] setBorderColor:[UIColor darkGrayColor].CGColor];
if (i==0) {
[button setFrame:CGRectMake(x, y, width, height)];
} else {
if (i%5==0) {
nRow++;
x=18;
[button setFrame:CGRectMake(x, (y*nRow), width, height)];
} else {
[button setFrame:CGRectMake(x+space+width, (y*nRow), width, height)];
x=button.frame.origin.x;
}
}
[[self view] addSubview:button];
}
The main part of this code is the first 6 lines, after is all x and y.
dataSource is an NSArray declared as property (nonatomic, strong). It contains UIImage objects.
You should be lazily loading your images in conjunction with reusing your buttons to account for the possibility of a large number of images.
To implement:
Keep the paths to the image files in your data array instead of the UIImage objects. Get the image from the path using imageWithContentsOfFile: when you need it.
Load the first z buttons into the scroll view where z is the number that appear on the screen at a time plus one row's worth.
Set the UIViewController that we are currently in as the scrollview's delegate, and respond to changes in offset by repositioning buttons and setting appropriate images and targets.
Also, if 7/8 images is crashing your app, it sounds like you're dealing with some pretty large image files. Try to provide thumbnail-sized versions of the content within the documents directory(whatever the size of your buttons are EXACTLY), or if images are dynamic, see this post for a how-to.
If you are probably using ImageNamed, this article helped me alot:
http://www.alexcurylo.com/blog/2009/01/13/imagenamed-is-evil/
Mainly
DO NOT USE [UIImage imageNamed] for any significant amount of images. It is EVIL. It WILL bring down your application and/or Springboard, even when your application is putting along using just barely a nibble of memory on its own.
and
it is better to implement your own cache
and here's the proposed cached image example:
- (UIImage*)thumbnailImage:(NSString*)fileName
{
UIImage *thumbnail = [thumbnailCache objectForKey:fileName];
if (nil == thumbnail)
{
NSString *thumbnailFile = [NSString stringWithFormat:#"%#/thumbnails/%#.jpg", [[NSBundle mainBundle] resourcePath], fileName];
thumbnail = [UIImage imageWithContentsOfFile:thumbnailFile];
[thumbnailCache setObject:thumbnail forKey:fileName];
}
return thumbnail;
}