I have so far managed to create an imageView with a picture in order to create a background. (This takes up the whole window) I now want to switch to several views using buttons that are on top of this image! So far I have found many tutorials that show how to create a custom view and have buttons bellow in the NSWindow, but am unable to get my head around how to do it if the button is actually within the custom view.
The aim is to have the start up view which has a button saying "start game" clicking it will swap to a new view which again will have numerous buttons which will again switch to various other views.
I am new to Xcode and to this forum so if it has already been described somewhere please don't have a go, just point me in the direction. I have searched far and wide so it is not down to lack of effort.
Set tag on button click..so that images can identified.
-(IBAction)editImage:(id)sender
{
UIButton theButton = (UIButton)sender;
if (tag==1) {
[theButton setImage:[UIImage imageNamed:#"image1"] forState:UIControlStateNormal];
[theButton setTag:2];
}
else if (tag==2){
[theButton setImage:[UIImage imageNamed:#"image2"] forState:UIControlStateNormal];
[theButton setTag:1];
}
}
Hope this will help you
Related
Specifically, I coded in facebook's login button into my view controller file. However it does not show up in storyboard. I know how to make a button in storyboard and then connect in code with right click drag, but when I try this to back into connecting the two and create my IBOutlet, I seem to just be creating two different login buttons.
How do I get the facebook login button to show up in storyboard?
import UIKit
import FBSDKCoreKit
import FBSDKLoginKit
class ViewController: UIViewController, FBSDKLoginButtonDelegate
{
override func viewDidLoad() {
super.viewDidLoad()
if (FBSDKAccessToken.currentAccessToken() == nil)
{
print("Not logged in..")
}
else
{
print("Logged in..")
}
let loginButton = FBSDKLoginButton()
loginButton.readPermissions = ["public_profile", "email", "user_friends"]
loginButton.center = self.view.center
loginButton.delegate = self
self.view.addSubview(loginButton)
}
In this case, you can't see the button in your storyboard. The FBSDKLoginButton is a custom button provided by Facebook. By using the FBSDKLoginButton, you bypass some extra code. It is possible to add a custom login button in the storyboard and use to code from the Facebook developers docs within the button's connected action.
Dan L is right. You can only manipulate and see objects in Storyboard that was added via the storyboard itself. However, you can see the UIButton that you added programmatically to the ViewController by viewing it in the View Debugger. This way you can rotate, zoom in, and focus on any element on the visible screen, even the Facebook Button. But this does not allow you to manipulate the objects within the view debugger. And most of the time you want to do that to set your auto layout constraints in relation to everything else on screen. You actually can still do this: but it requires a little imagination.
1) You must choose the elements in the view controller's storyboard (IBOutlets) that you want the button to be anchored to.
2) In code, mentally visualize where you'd want the UIButton to go. And for each direction of the button programmatic add NSLayoutConstraints to the surrounding views.
UIView *leftView = self.viewToTheLeftOfWhereIWantTheButtonToGo; //IBOutlet. Remember to list here all of the surrounding views like so
UIView *button = self.theFacebookButton; //IBOutlet
//Top | do for each direction
[container addConstraint:[NSLayoutConstraint constraintWithItem:button
attribute:NSLayoutAttributeLeft //button's Left
relatedBy:NSLayoutRelationEqual
toItem:leftView
attribute:NSLayoutAttributeRight //leftView's Right
multiplier:1.0
constant:kFacebookButtonPadding]]; //some CGFloat you define
// And do this for Left, Bottom, and Right also.
3) Because all of the subviews including the Facebook button are subviews to the self.view, tell the main view to layout it's subviews.
[self.view layoutIfNeeded];
4) Tweak this to your needs, Build and Run, and examine your results in the View Debugger. And that is the next best thing to using storyboards.
In many apps' introduction screens, you have a UIPageControl that sits on top of a fullscreen image (no black border at the bottom).
I can not figure out how this is done. Am I correct that the UIPageControl is added as a subview of the ChildViewController? how does the pagecontrol not move once the user swipes to the next screen?
Thanks
One way is to use a UIPageViewController as your main screen. Implement
-(NSInteger)presentationCountForPageViewController:(UIPageViewController *)pvc {
//
}
-(NSInteger)presentationIndexForPageViewController:(UIPageViewController *)pvc {
//
}
in your page view controller data source. (See the documentation to learn what you need to return for each of those methods.) The page control will magically appear, as part of the page view controller, simply because you implemented those methods.
Unfortunately it is difficult to see the UIPageControl, because the default is white on white! What I do is to color it with the appearance proxy. For example:
UIPageControl* proxy = [UIPageControl appearanceWhenContainedIn:[UIPageViewController class], nil];
[proxy setPageIndicatorTintColor:[[UIColor redColor] colorWithAlphaComponent:0.6]];
[proxy setCurrentPageIndicatorTintColor:[UIColor redColor]];
[proxy setBackgroundColor:[UIColor yellowColor]];
If you don't want to use a page view controller, you can add the UIPageControl directly. As you rightly say, it can't be a child of a child view controller, since it would then vanish when we switch to a different child. It has to be a child of the parent view controller, sitting in front of everything.
Ran into a snag and hoping for some insight here.
Overview: Universal app. The iPhone portion works great. The iPad portion has a split view controller. The master is a table view (left side) where the user selects a row and displays its detail (right side). Pretty standard stuff.
I’m using Xcode version 4.4.1 and my project is using Core Data, Storyboards and ARC.
DetailViewController: On the detail is a button that brings up another table view (within the detail) of user notes by date with a custom view. Each note can have an image associated with it. If a note has an image then a button with its photo icon is shown.
Example screenshot up to this point:
MWPhotoBrowser: When the button of the photo icon is pressed I call MWPhotoBrowser (a wonderful library to show images in a standard way) to display the image or images. Single tap shows the one selected image, a double tap shows all images on the current table with the one chosen being viewed first. The image is displayed full screen.
To show the photo browser I use this code:
// Create the browser view.
MWPhotoBrowser *browser = [[MWPhotoBrowser alloc] initWithDelegate:self];
// Set browser options.
browser.wantsFullScreenLayout = YES;
browser.displayActionButton = YES;
[browser setInitialPageIndex:self.buttonRow];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:browser];
[self presentModalViewController:navController animated:YES];
The navController allows me to include a navigation controller with a “Done” button which calls doneButtonPressed.
Issue: All of the above works well until I press the “Done” button. The full screen photo browser dismisses properly. Unfortunately, the table view on the right side from where the user pressed the photo icon button is now gone (black). I thought the NotesViewController (right side) would remain there after photo browser was dismissed modally. Does calling a model view controller in a split view cause the other view controllers to be deleted?
Example screenshot showing problem:
The stacks on the detail view controller (right side) should be DetailViewController > NotesViewController > then call MWPhotoBrowser modally. After the photo browser is dismissed my self.navigationController.viewControllers.count equals 1 and I was expecting it to be 2.
Here’s the code that dismisses the MWPhotoBrowser:
- (void)doneButtonPressed:(id)sender {
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
[self dismissViewControllerAnimated:YES completion:nil];
}
.
.
.
Hope someone can help me wrap my head around this.
As always, any help is greatly appreciated. Thanks!
I resolved my issue within the NoteViewController (the area turning black after the MWPhotoBrowser was dismissed).
Within viewWillAppear I was setting my tableview to nil (to prevent cell overwrap) and reloading my table data.
self.tableView = nil;
[self.tableView reloadData];
This works fine for the iPhone and when not returning from the MWPhotoBrowser’s full screen display on the iPad. For some reason that I do not understand, the above code causes the issue. I added additional logic to check if I’m returning from the full screen display and now all is fine. I’ll look into it more when time permits.
In my application, I have developed one wizard in which I am providing a way for the user to setup his details one-by-one. After finishing all steps, the user will be redirected to the screen where TabBar will come into the picture.
The problem here is that the user can access the same view controllers with the wizard (without TabBar controllers) and normal flow (which is with tabbar controller). In the wizard, I am using a view controller of size 320x480 and the same in normal flow. But whenever I load any view controller using TabBar the 44 pixel view from bottom side gets hidden behind TabBar.
I know the I can manually set the view size, by detecting whether TabBar is present or not, but here in this case number of view controllers are more and its already designed of size 320x480.
I had tried with all methods given in Apple's documentation but none of it seems to work for me.
Following are the methods I have tried, along with some xib settings.
[self setWantsFullScreenLayout:YES];
self.view.autoresizingMask = UIViewAutoresizingFlexibleHeight;
[self.view setAutoresizesSubviews:YES];
[self.navigationController.view setNeedsLayout];
Is there any way to set the height of a view controller according to whether that TabBar is present or not?
In my case, (Status bar/Nav bar/Hidden tabbar) this worked
//Add this in your ViewController
self.edgesForExtendedLayout = UIRectEdgeBottom;
self.extendedLayoutIncludesOpaqueBars = YES;
However Barry's answer is better if you use Storyboard but I could not for this VC.
Tried for iOs7 and iOs 8.
If you're using storyboards, select each tab VC and clear the box for View Controller > Extend Edges > Under Bottom Bars.
I wasn't able to find a good answer for this, so I ended up adding a BOOL, hasTabBar. I set it true or false based on where I create my view, and use that to calculate my layouts.
frame.size.height -= (hasTabBar*50); // works nicely.
I'm very new with iOS Development and I have just created one of my first apps, in my .xib file I have a UINavigationBar that I want to hide/show when a part of the screen is tapped by the user (like in the Photo app). I've found some snippets online but I don't know where and how to use those.
I'd appreciate a lot if somebody could give me detailed informations about how to do this.
Add this toggle method anywhere in your UIViewController. This hides on first tap and shows again in second tap.
- (void)toggleNavBar:(UITapGestureRecognizer *)gesture {
BOOL barsHidden = self.navigationController.navigationBar.hidden;
[self.navigationController setNavigationBarHidden:!barsHidden animated:YES];
}
If there is no navigation controller, link the navigation bar with an IBOutlet and replace with
- (void)toggleNavBar:(UITapGestureRecognizer *)gesture {
BOOL barsHidden = self.navBar.hidden;
self.navBar.hidden = !barsHidden;
}
Then add the following in the method -(void)viewDidLoad {}
UITapGestureRecognizer *gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(toggleNavBar:)];
[self.view addGestureRecognizer:gesture];
[gesture release];
If the view where you are going to tap is a UIWebViewController, you have to add the protocol to the view controller and set it as delegate gesture.delegate = self; then add the following:
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer
shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
return YES;
}
This is needed because the UIWebViewController already implements its own gesture recognizers.
Ultimately, you want to send the -setHidden: message to your navigation bar. The easiest way to do this is to make an Outlet and an Action in your in your view controller. Then, in your .xib file, connect the navigation bar to the outlet and some button (even a large, full screen one) to the action.
Outlets and Actions are basic techniques used over and over in iOS
(and Mac) programming, so if you don't understand them, best go read
up on them now. Every beginning iOS/Mac programming book covers this
topic as does Apple's own Getting Started guide (pay particular
attention to the Configuring the View section).
Inside your action, send a message to the outlet like so:
-(void)myButtonAction:(id)sender{
[[self myNavigationBarOutlet] setHidden:YES];
}
This will hide the navigation bar whenever your button is tapped.
(This assumes you have a UINavigationBar in your .xib like you say. These directions will be different if you're working with a UINavigationController that manages its own UINavigationBar)