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.
Related
I added an UIButton to my GameViewController in storyboard and set it to hidden.
Now I want to let that button show up from the GameScene
I have set the Restoration ID to "sharee" and I also set the Document label to "sharee", but when I use sharee.hidden = false in my GameViewController or GameScene, it says: "use of unresolved identifier sharee".
How can I interact with the UIButton I added in storyboard from my GameScene?
First you want to make a reference to the button when you assign the scene. You want to make sure you create an IBOutlet from your storyboard to your view controller as well.
In your game's view controller before you present the scene.
yourGameScene.button = _yourViewControllerButton;
Declare the button reference in your scene header.
#property UIButton *button;
Then in your implementation of the scene make the button hidden by typing...
_button.hidden = YES;
And make it appear by typing...
_button.hidden = NO;
Edit in more detail:
First you want to make sure that you have an IBOutlet connected to your view controller. The easiest way for someone starting out to do this is by switching to the assistant editor and then holding control button down and then clicking on your game button and dragging it into the interface section of the game view controller class. It will make a line. (Make sure it is the right view controller on your second view and make sure to choose outlet, not action, if it prompts.)
Secondly you want to add a property in your game scene header. You can name it whatever you want but for the tutorial sake just name it button and change it around later if you wish.
#property UIButton *button;
Third, when you created this game scene and started assigning properties, go ahead and set the view controller button reference to the game scene button reference.
Finally, do whatever you want to implement the hiding and the showing. For me I made it by toggling it in the touchesBegan method.
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 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.
I have a UIView and a ScrollView as 2 separate subviews. How can i drag a UIView(named "a") from my UIView and have the scrollView "take over" that dragged "a" UIView?
It really depends on what you exactly mean by 'take over' the UIView.
If you simply want to change the parent view, when your view is 'dropped' then remove it as a Subview of it's parent view & add the UIView as a Subview of the UIScrollview.
If the original ParentView has a property that holds the UIView, then it will still retain the instance but the view will be one level down the hierarchy.
If you want to past the entire instance of the view to UIScrollView, you'll have to subclass UIScrollView & add a property for the UIView. Then you can either create a custom setter (i.e. don't #synthesize) that will retain the instance & do the above view adding/removing
OR
Create a - (void)provideNewView:(UIView *)newView method that will assign to the local property and do the view adding/removing.
How would I go about implementing dragging and dropping a UIView from UIPopoverController into the back UIView.
This is the functionality that Pages provide in their insert media popover, where you can drag a shape out from the UIPopoverController and drop it into the main document.
I am actually confused with the pan UIGestureRecognizers and where they will be implemented.
Thanks,
Umer
According to the documentation on UIPopoverController, when the popover is presented, it is presented on a special "window". Because of this, simply adding a subview to the popover view controller's content view controller is not sufficient to be able to drag a view outside of the popover view controller's view.
The easiest solution here is to create your own window, add your drag-able view to the window when dragging occurs. Make the window visible for the duration of the drag/drop, and then release your window when complete.
As mentioned above, gesture recognizers (GR) are best suited for Drag/Drop functionality. Once the GR's state has changed to "Began" the GR will control all touches until the "Ended" or "Cancelled" state is achieved which makes it ideal for dragging views between view controllers as well as windows :)
Example:
#interface MySplitViewController : UISplitViewController {
UIView *dragView;
UIWindow *dragWindow;
}
Implementation:
NOTE we do not need to call "makeKeyAndVisible" on our window. We just need to set its "Hidden" property
From Apple in regards to the makeKeyAndVisible method:
// convenience. most apps call this to show the main window and also make it key. otherwise use view hidden property
-(void)dragBegan{
self.dragWindow = [[UIWindow alloc] initWithFrame:self.view.window.frame];
[self.dragWindow addSubview:self.dragView];
[self.dragWindow setHidden:NO];
}
Here we handle the Gesture Recognizer's "Ended" or "Cancelled" state.
NOTE: It is important to remove the window when the Drag/Drop is complete or you will lose user interactiveness with the views below.
-(void)dragEnded{
[self.dragView removeFromSuperview];
[self.dragWindow setHidden:YES];
[self.dragWindow release];
[self.view addSubview:self.dragView];
}
You have to deal with two view controllers one that's in the background called mainController one that presented using a UIPopoverViewController called popoverController. Your popoverController could add a UIPanGestureRecognizer to the views, that the user can drag. The action target of the gestureRecognizer could be a method on the popoverController.
Once the user starts a dragging operation your action method is called with the gestureRecognizer as an argument, were the state of the gestureRecognizer is UIGestureRecognizerStateBegan. You could than save the current frame of the view somewere to be able to animate it back, when the dropping fails. It might be necessary to move the view to an other superview (the window for example), because I'm not sure if UIPopoverViewController clipsToBounds its view.
As the user draggs, your action method is called over and over with the gestureRecognizer in the state UIGestureRecognizerStateChanged. Use the translationInView: method on UIPanGestureRecognizer to determine how much the user dragged and update the dragged views center/frame/transform accordingly.
Once the user lifts his finger the action method is called for a last time with the gestureRecoginzers state set to UIGestureRecognizerStateEnded. Now it's time to find out if the drag was successful. For example the popoverController could ask the mainController via delegation if there's a drop target under the views current position, if so the mainController can take action, else the popoverController would animate the dragged view back to were it came from, and add it back as a subview to it's view.
I hope this is somehow comprehensible and helpful.