iPhone + UIControls - iphone

It is a very generic question (not related to any application). Just my quest to know more.
While creating any iPhone application, we see that there few UIControls available in Library of Interface Builder. Using those we can create all basic application and designs, and also we can't customize those to much extent
But if we see on App Store, thousands of applications are available, where there many UIControls which are either not available in Library or might be they customized to much extent.
I want to know that:
Can we use controls other than available in Library (if yes than how and from where to get that).
If we can't than how do we modify (customize) the available UIControls to such an extent that they look totally different from their original primary look.
Regards,
Pratik

For a start, it is possible to set an image (including transparency) for most controllers. Many controller that look like different controls may actually be the same controls, just reskinned. Many controllers allow subviews - it is amazing how much can be achieved just using the table controller.
It is also worth knowing that any control you place using the GUI can also be set to a subclass where you override behaviour. Simply select the control, hit Shift-Apple i and change the class in Class Identity.

For customised controllers you can check the Three20 Project by Joe Hewitt.

take a look at the three20 library. It include custom styled button, labels and text. It should give you the general idea, how to customize your own UI elements.
Often, you don't use images like casebash mentioned, instead your drawing your elements with graphical methods.
Another possible solution is to override your
- (void)drawRect:(CGRect)rect
of your UIView
Edit:
Source Code taken from
Custom UIButton for Iphone
INDEX_OFFSET = 82753; // random
UIButton *sampleButton = [UIButton buttonWithType:UIButtonTypeCustom];
[sampleButton setFrame:CGRectMake(kLeftMargin, 10, self.view.bounds.size.width - kLeftMargin - kRightMargin, 52)];
[sampleButton setTitle:#"Button Title" forState:UIControlStateNormal];
[sampleButton setFont:[UIFont boldSystemFontOfSize:20]];
[sampleButton setTag:<INDEX>+INDEX_OFFSET];
[sampleButton setBackgroundImage:[[UIImage imageNamed:#"redButton.png"] stretchableImageWithLeftCapWidth:10.0 topCapHeight:0.0] forState:UIControlStateNormal];
[sampleButton addTarget:self action:#selector(buttonPressed) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:sampleButton];

Related

iPhone the beginner's application [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
Short pre-introduction to a problem:
I applied to a position of iOS developer. For this position they don't require knowledge of Objective-C or iOS development. The requisites are a good experience with C/C++ and development for Linux platform.
Problem:
After the end of interview I got a task (with words that it's easy to impelement, is it?) and I can't understand whether it's so.
Interviewer has showed me an application on his iPhone that looked like:
Pressing/dragging that button leads to appearance of such bar:
He could scroll it and drag on the screen. When he had pressed the control button, bar disappeared.
I want to ask for excuse in advance, because I feel myself like a person who wants his job to be done by others. But I just want understand how I can approach to this problem. Cocoa library is a big and a new beast to me. Which classes/elements from it is it better to use to reach this aim? I programmed before with Qt, Tkinter, WinAPI (gui programming) for desktop but it doesn't help me a lot here.
I need to code something similar like the interviewer has showed me. I tried to find something in the internet similar to this, but without success.
So, I just want to know for which classes it's better to look for in Cocoa library.
And, if I failed to take something into consideration I would be glad (happy) to hear your notes.
Thanks in advance for any replies!
SOLVED
I want to thank everyone who replied and didn't leave me to sink into this unknown sea.
After a week of exploring I got what I wanted:
Source code: (I apologize, but I can't copy-paste code from virtual mac os machine)
Now I understand how too-wide and silly is my question, but anyway perhaps it helps someone to start out. Also one book helped me a lot.
There is no required understanding of Objective-C, but the interviewer wants you to create a simple application and return it to him? Interesting interview.
As for your question. The classes I see being needed here (aside the AppDelegate and a UIViewController of your choice) are:
UIButton
UIScrollView
UIImageView
The button's use here is obvious. The scrollview is again obvious. The imageview would be the icons inside the scrollview.
Allright - just to give you some guidance on what to start with and how to proceed so that you don't get lost in the great wide world of cocoa ...
And assuming that you are familiar with programming and have some decent experience in the c++ and java worlds ...
Open xcode.
Go for blank view based app. Single view application would do. Go for Automatic Reference Counting (ARC).
xcode creates a template for you with an application delegate (which you would not need for this task)
A view controller is created and a xib/nib file for an iPhone view. (ignore the ipad view if any).
Add to your view controllers so called IBAction methods. One for the top button and one for the others. An action is supposed to be called when a button is pressed (and other events which you don't need).
Properly defined you will see that action in the interface builde when editing the xib file. Add a button and connect it with this action (touch up inside would be best I think).
Compile this. When your button is pressed the action is executed. Set a breakpoint there to make sure that it got called.
Now, in this action, you either call a newly created method showScroller and hideScroller. And create those methods.
Now it gets a bit more complicated.
In showScroller you would have to ...
create a UIScrollView. (alloc it and init it)
create a number of UIButtons. (no alloc withot its init in objective-c)
set your second IBAction method as target for the buttons.
Position the buttons within the UIScrollView accordingly.
Position the UIScrollView nicely under the top button.
add the UIButtons as subview to the contentview of the Scroll view.
add the scroll view as subview to self.view (that is the underlying view, the grey thing in the interface builder).
In hideScroller you would have to ...
remove the UIScrolView from the view. For that you could either remove all subviews from self.view or you would have to keep a reference to the scroll view within your view controller in some instance variable. Pretty similar to C++ and Java which you know already.
Alternative to the scenario described above you could create all views within interface builder and pre-define the scroll view as hidden in IB (interface builder) and in showScroller and hideScroller you would just have to set its .hidden property to YES or NO. But I've got a guts feelting that with your background you should and could do this programmatically instead of hasseling with how IB connects into your code.
In the event that you really run into that trap and don't get the top most button properly connected to your code, then create that button programmatically too.
Ah, I forgot. The right place to create all those UI elements would be the viewDidLoad method of the ViewController class. There is an empty one generated already in your empty xcode template. Just enance it following the [super viewDidLoad]; call.
Have a look at this:
http://www.cocoacontrols.com/platforms/ios/controls/aurosetteview
When you press a button, the items show up and when you press again, it closes.
The source code is within the library. Use it to read up.
For your case, you need to include the scrollview as well.
Here you go:
Open XCode and create a new project. Choose iOS Empty Application template.
Go to menu, choose File, New File, Objective-C class. Name it RootViewController and choose a subclass to be UIViewController
Go to your app delegate .m file (If your project name was Test, this file is TestAppDelegate.m). At the top of the file, below the line where it says #import "TestAppDelegate.h" add another line that says #import "RootViewController.h"
In the same file there is a method named:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
at the end of the method but before return YES; statement add the following line:
self.window.rootViewController = [[RootViewController alloc] init];
Now go to your RootViewController.m file that you created in the step 2. and at the top of the file edit the interface to have this one instance variable:
#interface RootViewController ()
{
UIView *menuView;
}
#end
In the same file locate the method named - (void)viewDidLoad and add the following code in it: - (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor =[UIColor blackColor];
UIButton *menuButton = [UIButton buttonWithType:UIButtonTypeCustom];
menuButton.frame = CGRectMake(100, 60, 50, 50);
[menuButton setBackgroundImage:[UIImage imageNamed:#"menu.png"] forState:UIControlStateNormal];
[menuButton addTarget:self action:#selector(menuButtonTapped:) forControlEvents:UIControlEventTouchDown];
CGRect menuFrame = CGRectMake(20, menuButton.frame.origin.y + 50, 300, 60);
menuView = [[UIView alloc] initWithFrame:menuFrame];
menuView.backgroundColor = [UIColor purpleColor];
menuView.hidden = YES;
UIButton *menuButtonA = [UIButton buttonWithType:UIButtonTypeRoundedRect];
menuButtonA.frame = CGRectMake(5, 5, 50, 50);
[menuButtonA setTitle:#"A" forState:UIControlStateNormal];
[menuView addSubview:menuButtonA];
UIButton *menuButtonB = [UIButton buttonWithType:UIButtonTypeRoundedRect];
menuButtonB.frame = CGRectMake(65, 5, 50, 50);
[menuButtonB setTitle:#"B" forState:UIControlStateNormal];
[menuView addSubview:menuButtonB];
UIButton *menuButtonC = [UIButton buttonWithType:UIButtonTypeRoundedRect];
menuButtonC.frame = CGRectMake(125, 5, 50, 50);
[menuButtonC setTitle:#"C" forState:UIControlStateNormal];
[menuView addSubview:menuButtonC];
UIButton *menuButtonD = [UIButton buttonWithType:UIButtonTypeRoundedRect];
menuButtonD.frame = CGRectMake(185, 5, 50, 50);
[menuButtonD setTitle:#"D" forState:UIControlStateNormal];
[menuView addSubview:menuButtonD];
UIButton *menuButtonE = [UIButton buttonWithType:UIButtonTypeRoundedRect];
menuButtonE.frame = CGRectMake(245, 5, 50, 50);
[menuButtonE setTitle:#"E" forState:UIControlStateNormal];
[menuView addSubview:menuButtonE];
[self.view addSubview:menuButton];
[self.view addSubview:menuView];
}
Below that method add a new method:- (void)menuButtonTapped:(id)sender
{
if (menuView.hidden)
menuView.hidden = NO;
else
menuView.hidden = YES;
}
Find some nice png image on the internet that will represent your menu button. Name it menu.png and drag and drop it in your project(inside the "file" menu on the left side)
That's it run the project ;)
just read the iphone's beginner book. you have to read some book first. first try to understand basic controls in iphone & basic manipulation in objective c
your above work is very basic.now just you are away from one step and that is read a book, you will get lots of free pdf on net

How do you style iOS components?

I am just getting started in iOS development after working in Android since the early days and I am a bit confused on how you style/skin iPhone components.
I saw that there is a way to change the color for some components (is that what they are called?) in properties but how can I go even further?
For example, how could I be designing a custom button, or custom listview?
There are several elements here you are asking, so my answer might not be enough for everything you want.
You can for instance use this page here that give you a real nice tutorial for doing some customization to UI components:
http://www.raywenderlich.com/4344/user-interface-customization-in-ios-5
For UITableViews there are other steps you would have to take. For instance, this very good tutorial:
http://cocoawithlove.com/2009/04/easy-custom-uitableview-drawing.html
If you create custom UI elements to replicate standard UI elements in
all but appearance, you should instead consider using the appearance
customization programming interfaces available in iOS 5 and later.
via HIG
Which i suggest viewing this to get the best understanding of the interface builder
For a Button as an image:
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *image = [UIImage imageNamed:#"myFile.png"];
button.frame = CGRectMake(x, y, image.size.width, image.size.height);
[button setImage:image forState:UIControlStateNormal];
[myView addSubview:button];
or with the interface builder:
select "Custom Button" as the type and then select the image you want
from a drop-down menu.

Custom UIButton iphone

I want to change the color of my UIButton. I did this:
UIButton *alreadySubscriber = [[UIButton alloc] initWithFrame:CGRectMake(12, maxy-65, 262.0, 50)];
alreadySubscriber.layer.cornerRadius = 10.0f;
[alreadySubscriber setBackgroundColor:[UIColor orangeColor]];
[alreadySubscriber setTitle:#"Already a subscriber" forState:UIControlStateNormal];
The color of the button changes, but no longer see the effect of bright light booby top. how can I fix this?
We're unable to programmatically set this 'bubble effect' for the UIButton I'm afraid. The only way to go about it far as I can tell is to go for a custom button with an image (or two images for the normal and active states).
The resource Eugene put up earlier seems pretty good in fact.
I don't really know what kind of booby light are you talking about, but have you tried using images for your buttons? I'd suggest using two different images like those you can create here http://dabuttonfactory.com/.
You should create as a custom button and add the image/color as background.

Inverting UIBarButtonItem "Play" for use as a Back Button?

Okay, simple enough.
I'm coding up a simple web view, and I'm providing a few Safari-like controls for navigation.
Play seems to be the obvious choice for the forward button, but I'd like to have a Back Button as well, as seen in several Apple and third party apps.
Is there a way to invert the icon, so that it points backwards, or are all the apps using this setup using images to replicate this functionality?
Unicode is your friend here.
Create a UIBarButtonItem (in Interface Builder or in code) and set it to "Custom" where you can enter text for the button.
Now use these Unicode characters to simulate the back & forward buttons:
◄ and ►
I use it in my apps and it looks great.
I know this was answered long ago, however just to add...
Yes, you can use Unicode but it does not look the same as the standard ios button. And if you have strict requirements to match ios look than you will need to at least make an image for the back button, in this case by simply downloading the play image and flipping it.
UIImage *image = [UIImage imageNamed:#"backButton.png"];
UIButton *btnBack = [UIButton buttonWithType:UIButtonTypeCustom];
[btnBack setImage:image forState:UIControlStateNormal];
btnBack.frame = CGRectMake(0.0, 0.0, image.size.width, image.size.height);
UIBarButtonItem * btnItem = [[UIBarButtonItem alloc] initWithCustomView:btnBack];
[btnItem addTarget:self action:#selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];
// add it to your bar
self.navigationItem.leftBarButtonItem = btnItem;
(wrote it without spell checking... so beware of spelling errors that may have resulted)
// button images are here.
http://developer.apple.com/library/ios/#documentation/userexperience/conceptual/mobilehig/IconsImages/IconsImages.html
You could use atPeek to extract the play button png from an app that uses it, and then use any image manipulation program to flip it.

Setting up layout/events on iPhone

I am using Open Source toolchain to compile my iPhone apps. So I have no Interface Builder or XCode. How would I setup the layout of widgets like UIButton, UITextView, etc. Also, how would I add an event handler to those UI widgets? Please remember that I don't have Interface Builder or XCode.
I suspect you're going to run into trouble eventually if you're not using Xcode. However, it is definitely possible to do everything iPhone without using Interface Builder. You just end up defining a lot of rectangles and pixel constants in your code, and calling (e.g.) addTarget to hook actions up to methods.
UIButton *myButton = [[UIButton alloc] initWithFrame:CGRectMake(MYBUTTON_X, MYBUTTON_Y, MYBUTTON_WIDTH, MYBUTTON_HEIGHT)];
[myButton addTarget:self action:#selector(foo) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:myButton];
[myButton release];
That creates a button and has it call your "foo" method when pushed. Anything that can be done in IB can be done in plain code (though it's often more tedious to do it that way.)