I have a uiimageview within a uiview and I would like it to not rotate when uiinterfaceorientationdidchange is called but I would like everything else to rotate. Right now everything is rotating, how can I set certain objects not to rotate?
A UIImage has a property imageOrientation. Or, make a custom view controller with only a UIImageView and in the shouldRotateToInterfaceOrientation: method of thatcontroller return NO. Then, in the interface builder for your main view controller, add a custom object and change its class to your custom UIImageView. Or you can add it as a subview programatically.
Check out the UIViewController Class Reference for more info.
Related
I'm just learning iOS programming, so sorry if this is a dumb question.
I have a view in a xib that's acting as an overlay, but I want that view to be "transparent", so that people can manipulate (tap) the views below it. I read that pointsInside:withEvent will do it (if set to return NO), but where do I put this method?
I have a viewController that owns my xib, but putting the method there doesn't do anything...
How do I add my method to a xib view? Do I have to make another view (programmatically) and add my overlay xib as a subview?
Thanks
You can set userInteractionEnabled to NO on the view instead. Overriding pointInside:withEvent: is really for modifying the "shape" of the view.
If you do want to override pointInside:withEvent: you will need to make a UIView subclass and do it there. However you can still add this view inside your xib. Select the view in the xib, and in the Identity Inspector pane set the class to your subclass.
I'm experimenting with a TabBarController and the default project creates the UITabBarController and also gives you two view controllers.
I want the view of one of these view controllers to be a UIScrollView, i.e. when calling self.view on FirstViewController I want to get back a UIScrollView * and not just a UIView *.
The view controller gets initialised with initWithNibName: but I can't see anything assigning the view property in there.
If this all sounds a bit weird, maybe I'm doing this wrong? I realise I can drop a UIScrollView onto the view that's already created with me, but it just seemed a bit pointless to have a parent view in this case.
Thanks in advance.
Ok, just realised how to do this.
I can do a cast in my code to make UIView a UIScrollView. Like so...
UIScrollView *tempScrollView = (UIScrollView *)self.view;
tempScrollView.contentSize = self.view.bounds.size;
Then, in Interface Builder, you can use the inspector to set a custom class for your UIView. I set the class as UIScrollView in here and all seems to work!
If you want to use interface builder. Just load up your nib, delete the view on the left panel, and drag a UIScrollView into the area.
Next link from Files Owner to the new UIScrollView as the view property.
The only downside to doing it this way is in your code, whenever you want specific UIScrollView functions you will have to typecast the view property (using (UIScrollView *)self.view ), or put it in a variable like so
UIScrollView *sview = self.view;
//Then use sview for your changes
The best way would be to do it in code however.
I am writing a simple app where I have a UIImageView that when clicked on, should open a UIImagePickerController, and once an image is selected, is should just assign that image to the UIImageView
I started by sub-classing UIImageView, and giving it a touchesBegan which would open an ImagePicker.
My question is, since only a UIViewController can call presentModalViewController to show the imagePicker, how should I go about this?
It's bad design to have your a view reference it's controller (though I guess my imageview is a subview). So I don't know how I'd pass the message up to the controller.
The other option is to detect touches in the controller to begin with, but then the only way I can see to tell if the touch was on the imageview is to actually test if the touch is within the frame of the imageview. This approach seems clunky to me... Am I missing something obvious?
Any thoughts on which method to go with, or any suggestions on a better method would be much appreciated! Thanks!
You don't necessarily need to subclass UIImageView to do this. You can have it as a subview of your main UIView for some UIViewController, and add a UIGestureRecognizer to the UIImageView. Then the method that tapping or swiping the UIImageView triggers will be in your UIViewController, so you can easily present a modal view from there.
When you init the UIImageView subclass, pass a reference to the view controller that loads the image view.
Then, your touchup inside on the UIImageView can call presentModalViewController with that view controller.
The Objective C way of doing this is to declare a custom delegate that your view controller implements. So for your case, it would a have a single method, something like viewTapped. Have your custom class call this delegate method whenever it is clicked.
Then make your view controller implement this delegate and launch the modal view controller when the delegate method is called.
I am trying to put a button in the corner of my MKMapView to control whether the map stays locked on to the user's location. What I have in mind is to create a UIView with a button on and add it over my MKMapView (not as an annotation or something) I can't figure this out with Interface Builder.
How can I add this button programmatically?
Controlling whether it actually follows the user etc. is already sorted - just need the button for it.
It looks like you are directly setting the controller's view outlet to an MKMapView object rather than a UIView object containing the MKMapView object. You cannot drop the button in such case on top of the MKMapView object in the IB. There are two ways you can deal with this,
Declare an outlet for the button and drop the button in the IB. This needn't be on top of the MKMapView object. Set the outlet to, say, a button property. Then in viewDidLoad do [self.view addSubview:self.button]; after setting the button's frame. (or)
Drop a new UIView object in IB and put the MKMapView object inside it. Set the controller's view to this container UIView object. Later drop the button on top of the MKMapView object and set it to its appropriate location.
I have already built a UIViewController subclass with a bunch of controls in it, and just realized that if I rotate the iPhone, half of the controls become invisible. So, I would like to somehow make the UIViewController's UIView scrollable so that when the device (or the Simulator) rotates, the user can scroll the view to see all the controls.
I was hoping to do this all in Interface Builder. I tried to change the class of the view from UIView to UIScrollView in the Class Identity editor, but nothing scrolls. The base class of my view controller is a simple UIViewController <UIScrollViewDelegate>.
Is there an easy way to make the main view in my view controller scrollable without having to recreate the whole thing in IB?
For people who are using storyboard, this is quite easy to do
in Document Outline select the topmost View of the UIViewController
then in Identity Inspecter, under Custom Class, for class enter UIScrollView
That's it.
Add a UIScrollView and make all your controls and widgets and labels subviews of the scroll view by dragging them from wherever they are "into" or "inside" the scroll view. This makes them subviews of the scroll view, which is what you want.
You can do this as follows:
Open your nib file and Create a UIScrollView object under your UIView Object.
Move all your controls onto the UIScrollView
Open your header file (.h) and add a new property for the scroll view:
#property (nonatomic, retain) IBOutlet UIScrollView *scrollView;
Open your implementation file (.m) and insert the following:
#synthesize scrollView;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
// Enable scrolling for portrait
scrollView.contentSize = CGSizeMake(scrollView.frame.size.width, 600);
}
Go back to your nib file and wire up the scrollView on the file's owner to your UIScrollView object.
The real trick here is setting the scrollView.contentSize. Once this is set, the scrolling should occur.
If you wanted to enable scrolling for when the device is in portrait orientation, you could use this:
// Enable scrolling for landscape orientation
scrollView.contentSize = CGSizeMake(scrollView.frame.size.width, scrollView.frame.size.height);
This should give you some frame of reference.
Flea
It would be helpful to understand what your interface is displaying, but I'd suggest one of the following:
Don't allow rotation by returning NO from shouldAutorotateToInterfaceOrientation
Use Interface Builder to adjust your springs and struts so that all of your interface elements fit in landscape view
Add a new UIScrollView in Interface Builder and drag your UIView into it, then re-assign the view property of your File's Owner to the scroll view.
I was able to do this by adding a scroll view to the view in IB, making the view controller a UIScrollViewDelegate, hooking up the scrollview to a UIScrollView object in IB, hooking the delegate up to file owner, and adding this line in the viewDidLoad:
- (void) viewDidLoad {
scrollView.contentSize = CGSizeMake(1280, 960);
}
Scrolling behavior is only invoked if the contentSize of a UIScrollView is larger than its bounds.
Add a Scrollbar into Xib file and then drag & drop controls in to the Scrollbar. Inside load view use
scrollView.contentSize = CGSizeMake(CONTENT_WIDTH, CONTENT_HEIGHT);