Customizing Custom UIView class - objective-c

I have a custom UIView class that creates a view I can use in my main view controller that allows for better dragging. The problem, is that right now, the object I'm dragging is just a black box. I would like to be able to put my own custom image into that box. How would I do this? The custom UIView only has a .h and a .m and I don't know which method to put the code in.
Here's the code Im using right now
UIImageView *player = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 37, 37)];
player.image = [UIImage imageNamed:#"ghost.png"];
[self addSubview: player];

You could
Add your custom view in your view controllers main xib file and add the image view to it in the interface builder view
In your view controllers viewDidLoad method you could add the UIImageView to the custom view using your current code
Override the drawRect method of your custom view and draw the image there see here (however note the answer which says you should not load the image in the drawRect method, load the image on construction of your view in the initWithFrame or the awakeFromNib method depending on how you load it)

Related

Navigation Controller .. Table view Background image

When I add a Navigation Controller, I have two views: A Navigation Controller and a Table View.
I want to add static cell in the Table view. The Table view class is UITableViewController. How can I add a background image to the Table View Controller?
I have no Class where I can write in the code. I am a Beginner in Xcode. Where can I add the code:
UIImageView *bg = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"MyImage.png"]];
[tableView setBackgroundView:bg];
http://i.stack.imgur.com/zgJYk.png
or where there is an example?
thanks for your help
I don't think you can add a background image to a UITableViewControll from storyboard. What you have to do is create a custom class for your UITableViewControll and add the code in the ViewDidLoad method to add your background.
Create A Custom Class
dropbox.com/s/zqh5zu02f2dt0zo/create_custom_class.png
Assign your custom class to your Storyboard tableviewcontroller.
dropbox.com/s/5l94b9htfa1mz2w/add_custom_class.png
Add your code in the ViewDidLoad for your background image.
dropbox.com/s/2kz6m4iku27kyxz/code.png
Your app should now have a custom background behind the tableview.
dropbox.com/s/ks4riraqblaluoy/app.png
I hope that helps.

How to add UIView as a subview on UIViewController in UIStoryboard

Is there any way that I can add uiview as a subview over a view controller using UIStoryboard, using xib's we can do that, but i'm unable do that using storyboard.
My storyboard is not holding any of the uiview as a subview when I drag and drop on it, it was placing under the view controller.
Is there any way that I can add it programmatically on my view controller using storyboard.? I'm stuck please help me out
Am I right, you want to hold UIView in storyboards without view controller or superview ?
You can't do that. You should use XIBs to hold custom views.
It doesn't matter you add it programmatically or via drag and drop, in storyboards you can't hold "isolated" views, every view must have a superview and therefore a UIViewController.
Check apple's guide, make sure you understand UIViewController,UIView,UIStoryboard classes and relations between them. Also this.
Hope it helped.
Yes, you can override UIViewController's loadView method to do it as i have written code below.
Because loadView is the method which is called first of all other viewController's loading methods. So you can set it here.
Hope this will work for you as I have tested it on my code.
- (void)loadView {
self.view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, [UIScreen mainScreen].bounds.size.height)];
self.view.backgroundColor = [UIColor blackColor];
// enter your customization code here
}
write this line in function
-(void) ViewDidLoad:
[self.view addSubView:...];

Should I not override -[UIViewConvtroller view]?

I'm told not to override -(UIView *)view ever. Why not? I want to use a custom UIView subclass for my custom UIViewController. What's a better way to do so?
You're not supposed to override -view. To accomplish the behavior you want, you can set up your view controller to load from a nib, or you can override the -loadView method. In the latter method you create whatever view you want and assign it to self.view.
If you're using a nib or storyboard, simply set the root view's class to your new, custom UIView subclass. If you're not using a nib or storyboard, create your custom subclass in the -loadView method and set the view controller's view property to it. So, for example, if you had a custom UIView subclass named MyView, and you're creating it in code (not in a nib or storyboard), you'd do something like:
// This code sample assumes compiling with ARC
- (void)loadView
{
// You should adjust the initial frame to be whatever's appropriate for this
// view controller
MyView* view = [[MyView alloc] initWithFrame:CGRectMake(0, 0, 360, 480)];
[self setView:view];
}

frame of Root ViewController's View when loaded programmatically

Im loading a root view controller in landscape mode at launch(no interface builders are used).
In viewDidLoad, I am adding subviews to root view controllers view, like this
- (void)viewDidLoad
{
[super viewDidLoad];
// self.view.
UIView *toolBar=[[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 50)];
toolBar.backgroundColor=[UIColor darkGrayColor];
[self.view addSubview:toolBar];
//code contiues...
}
but self.view.frame.size.width returns width of portrait mode instead of landscape.
thanks in advance
EDIT:
Implement the -loadView method:
- (void)loadView
{
self.view = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
}
Also normally the parent view controller is responsible for setting the frame size of its children view controllers views. In the case of the root view controller, I believe it takes the size of the UIWindow it's attached to (so if you set the window size in you app delegate, you can just use [[UIView alloc] init] and then set the autoresizing mask in the -loadView method).
You might need to take the status bar into account in the above code, depending on your own code.
may be, portrait orientations are first in your project file (click to project in xcode, info tab in target, unit Supported interface orientations) ? If so, your app can launched in portrait orientation, then send viewDidLoad and rotate to landscape only after this.
When you do not use Interface Builder/xib files and neither create your view manually within loadview, the systems creates one with the maximum dimensions. This is stated in the loadview method documentation:
If the view controller does not have an associated nib file, this
method creates a plain UIView object instead.
This system generated UIView object has a transform property that is not the identiy transformation and thus you are not allowed to rely on the values from the frame property as stated here:
http://developer.apple.com/library/ios/documentation/uikit/reference/UIView_Class/UIView/UIView.html#//apple_ref/occ/instp/UIView/transform
Warning: If this property is not the identity transform, the value of
the frame property is undefined and therefore should be ignored.
So, what should you to instead? I guess the best solution is to use the bounds property of you UIView. This contains the already rotated coordinates.

Cocoa - Display xib on another xib

Can anyone tell me how (or direct me to info on) displaying a .xib (nib) on another .xib (nib).
How ever I wish to place it so I can programically move it around the main nib sort of like this (which obviously doesn't work)
- (void)drawRect:(NSRect)dirtyRect
{
NSRect customView = NSMakeRect(pos1, pos1, 200, 100);
[[NSBundle mainBundle] loadNibNamed:#"secondXib" owner:self];
NSRectFill (customView);
}
And I wish to do this for Mac OS X (Not iPhone). (By the way using xCode 4 incase it makes a difference)
You can easily load a view from another nib using NSViewController. In your nib you should just set File's Owner's custom class to NSViewController and hook up the view outlet of File's Owner to point to the view you want to load. You can then just do this:
//create an NSViewController and use it to load the nib
NSViewController* vc = [[NSViewController alloc] initWithNibName:#"YourNibName" bundle:nil];
//get the view from the view controller
NSView* loadedView = [vc view];
//release the view controller so we don't leak
[vc release];
//add the view as a subview of your main view
[mainView addSubview:loadedView];
//position the view
[loadedView setFrameOrigin:NSMakePoint(100.0, 100.0)];
You don't need to do anything in drawRect:. The subview will draw itself, and drawRect: will be called automatically if you move the subview.
You should read the View Programming Guide for Cocoa. It is critical to understanding how views work, and it is clear from your question that you do not yet have that understanding.
You should also read the Cocoa Drawing Guide.
Thanks a lot,
Another alternative ( which is basically the non programming way of doing it ), is to add a NSViewController object in your first xib, and set it to you use the nib name that you specify.
In your second xib, don't forget to set the class name in the "custom class" field on the view ( and NSViewController on file's owner ) else that won't work.