I'm having trouble positioning a MPVolumeView. In the viewDidLoad method of my viewController I have this code:
[super viewDidLoad];
MPVolumeView *myVolumeView = [[MPVolumeView alloc] initWithFrame: self.volumeView.bounds];
[self.volumeView addSubview: myVolumeView];
In the storyboard I've added the volumeView and I have hooked it up to the controller.
I'm including two screenshots: one of the storyboard and one of how the view looks when it is running on my phone.
How can I position the MPVolumeView in the volumeView?
The simplest way to do this is to create an MPVolumeView in Interface Builder instead of in code. You can do this by dragging a custom UIView into the XIB, configuring it as needed, and then changing its class to MPVolumeView.
Related
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:...];
I've just created a simple subclass of UINavigationController.
#import "TTNavigationController.h"
#implementation TTNavigationController
- (void)viewDidLoad
{
[super viewDidLoad];
UIImage *logo = [UIImage imageNamed:#"logo"];
UIImageView *logoView = [[UIImageView alloc] initWithImage:logo];
self.navigationItem.titleView = logoView;
}
But when I run the project the navigation bar has not the titleView with the logo.
Otherwise if I try to move those 3 lines of code in viewDidLoad of my view controller works fine. Why?
You can't use self.navigationItem because the navigation controllers own navigationItem is never used. It has this property because it is a type of view controller but it isn't actually displayed because the viewControllers (or, more specifically, the topViewControllers) navigationItem is displayed.
So, change the view controller navigationItem (either directly, or when it is pushed).
I set an image like this to my view:
self.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"map3.jpg"]];
Right now I have it in a button action method. But how do I make it set directly when the app is launched?
If you want to set the backgroundColor of a UIViewController's view, then move your code into viewDidLoad and adapt likewise:
self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"map3.jpg"]];
A UIViewController doesn't have a backgroundColor directly, but its view does, like any other UIView.
One way is to put that logic within your view controller's -viewDidLoad method for the UIImageView property.
UIView
If you are in a UIView class place the code in either initWithFrame: or initWithCoder:
initWithFrame gets called when you instantiate the view from code (e.g. UIView *myView = [[UIView alloc] initWithFrame:CGRectZero];
initWithCoder: gets called whenever the UIView is loaded from an XIB file.
http://developer.apple.com/library/ios/#documentation/uikit/reference/uiview_class/uiview/uiview.html
UIViewController
If in a UIViewController a common place to put it would be in viewDidLoad
http://developer.apple.com/library/ios/#documentation/uikit/reference/UIViewController_Class/Reference/Reference.html
I have a problem on how to properly do a certain kind of action.
The image below shows a UIViewController, but the second part of the view is a custom UIView (the one with the profile pic, name and Show View button).
The subclassed UIView is allocated using this code:
profileView = [[GPProfileView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 70)];
profileView.myTripGradientColor = YES;
[self.view addSubview:profileView];
The problem is of course, that the button on the UIView can't show any view, since it's only the UIViewController that can push another ViewController to the window(correct?).
The UIView is used in several places in the app and needs to be added easily and have the same behavior across the application.
This worked great until I added the button, and I'm starting to think I've made this wrong, and there has to be a better way to do it (maybe change the UIView to something else?).
I was thinking I should be able to call:
self.superview
And then somehow get the ViewController so I can push another ViewController into the view hierarchy, but nope.
Any suggestions and a tips on how to do this correctly?
UPDATE:
I have no idea on how to push another UIViewController from the button.
What should I do in this method when pressing the button in the UIView:
- (void) showViewButtonTouched:(UIButton*)sender {
GPProfileSocialFriendsViewController *friendsSettings = [[GPProfileSocialFriendsViewController alloc] init];
}
How do I push GPProfileSocialFriendsViewController?
Your - (void) showViewButtonTouched:(UIButton*)sender method should be in your controller and would probably be better named - (void) showView:(UIButton*)sender or - (void) showProfile:(UIButton*)sender so it clearly denotes what it does (not how you got there).
It's not the view's responsibility to manage transitions from a state to another. If you move your method to your controller, your problem is no more (you can easily access self.navigationController or push directly if you don't have an navigation controller like this:
[self presentViewController:vcThatNeedsToBePushed animated:YES completion:nil];
I think you can create weak reference in GPProfileView on UIViewController. Like this:
#property (weak, nonatomic) UIViewController *rootController;
when you create GPProfileView, assign rootController-property:
profileView = [[GPProfileView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 70)];
profileView.myTripGradientColor = YES;
profileView.rootController = self; // if view created in view controller
[self.view addSubview:profileView];
and in implementation of button selector:
self.rootController push... // or pop
May be this not correct, but you can try
You could let the view controller push the next view controller when the button is pushed. The view controller can add a target/action on the button, so that the action method in the view controller is called on the touch up inside event.
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.