UISegmentedControl within UIView does not respond to change event - ios

I have programmatically created a basic app on iOS7 SDK :
Created two UINavigationController
Added a view to each of them
Created a UITabBarController with two items
Added each UINavigationController to its respective UITabBarController
This is working fine, and everything is done programmatically. My project only consists in a simple window app template, without any xib nor storyboard, and everything is done within application didFinishLaunchingWithOptions: of my AppDelegate.m.
I am trying to add a UISegmentedControl (still programmatically) as a subview of the view of the first UINavigationController, as follows :
// First navigation controller
UINavigationController *firstNavController = [[UINavigationController alloc] init];
firstNavController.navigationBar.barTintColor = [UIColor redColor];
firstNavController.title = #"First";
firstNavController.navigationBar.topItem.title = #"First top";
firstNavController.tabBarItem.image = [UIImage imageNamed:#"FirstTab"];
// First view
UIView *firstView = [[UIView alloc] init];
firstView.backgroundColor = [UIColor yellowColor];
// Add a segmented control to this view
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:
[NSArray arrayWithObjects:#"One", #"Two", nil]];
// Register an event handler
[segmentedControl addTarget:self action:#selector(segmentedControlSelectedIndexDidChange:)
forControlEvents:UIControlEventValueChanged];
// Shape it and select its first item
segmentedControl.frame = CGRectMake(10, 100, 300, 20);
segmentedControl.selectedSegmentIndex = 1;
// add the segmented control to the view
[firstView addSubview:segmentedControl];
// and the view to the first tab's navigation controler
[firstNavController.view addSubview:firstView];
The UISegmentedControl DOES show up, but touch events are not handled.
If I change my code and add the UISegmentedControl directly within the UINavigationController, touch events are actually responding :
// and the segmented control directly within the navigation controller
[firstNavController.view addSubview:segmentedControl];
What am I doing wrong, and what should I do to have it respond to it's touch events ?

You need to set the size of the view.
Confusingly a view will be visible even if it is too small to fit it's content but will only respond to touches over the area of it's offical size.

You should not be adding a subview directly to the navigation controllers view.
'firstView' should be encapsulated into a UIViewController. And create your navigation controller with [[UINavigationController alloc] initWithRootViewController:myFirstViewController];

Related

Subclass viewController in storyboard and programmatically change UI from subclass

What I'm basically trying to do is have a storyboard with a navController and a couple views which are set up programmatically - or at least have subviews that are added programatically. So I've set up an example workspace, I have a navController and 2 viewControllers that are pushed depending on which button is pressed.
Here's the storyboard:
As you can see, the viewController on the bottom has it's class set to CVViewController. In CVViewController, I have my viewDidLoad as
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIView *redColor = [[UIView alloc] initWithFrame:self.view.window.frame];
redColor.backgroundColor = [UIColor redColor];
[self.view addSubview:redColor];
}
But then when I segue into this view on the simulator, there's no red background that should be there (because I added it programmatically).
So I'm not sure where I'm going wrong. I basically just want to be a able to configure a storyboard viewController programmatically from a custom class
Change this:
UIView *redColor = [[UIView alloc] initWithFrame:self.view.window.frame];
to this:
UIView *redColor = [[UIView alloc] initWithFrame:CGRectMake(0,0,self.view.frame.size.width, self.view.frame.size.height)];

How can I add a table view controller to an existing storyboard view controller?

I'm creating a landscape iPad app for iOS 5.1 that should have two table views embedded into a view controller of my storyboard. What I would like to be able to do, is drag a table view controller onto the view controller in my storyboard. But of course, Xcode does not allow this. I can drag a table view and get its data hooked up and it works properly, but then I cannot push a new view controller to replace that table when a row is selected.
I cannot use the 'Editor > embed in > navigation controller' trick, because then the entire storyboard view controller (which contains my two table views) is embedded. That is not what I want.
There must be a way to do this programmatically, but I can't seem to get the right combo of voodoo and science to make it work.
I have tried to create a custom container view to hold my tableViewController, but the table isn't showing up.
Any thoughts?
- (void)viewDidLoad
{
[super viewDidLoad];
CGRect frame = CGRectMake(68, 187, 402, 474);
_containerView = [[UIView alloc] initWithFrame:frame];
_containerView.backgroundColor = [UIColor redColor];
[self.view addSubview:_containerView];
categoryController = [[UITableViewController alloc] init];
categoryTable = [[UITableView alloc] init];
categoryTable.delegate = self;
categoryTable.dataSource = self;
[categoryController.view addSubview:categoryTable];
[_containerView addSubview:categoryController.view];
}
What you need is Container Views
Here are some tutorials that can help you:
http://www.cocoanetics.com/2012/04/containing-viewcontrollers/
http://weblog.invasivecode.com/post/12383262201/container-view-controllers-part-i-one-of-the
https://developer.apple.com/videos/wwdc/2011/?id=102

IOS- Pop-up with storyboard

I'm using storyboard and i want to add a popup to my app.
My popup will contain a slider (which i need to get his value), and also cancel option ('x' at top of the popup).
What is the best way to do that?
ViewController?
Xib file?
View?
Thanks in advance!
I think it's better to create a popup, button & slider in your code. Than you have more control over it. It's easier to access your slider & to dismiss the popover in the same code.
Put this code in the IBAction function when a button is pressed from where you want the popover:
//Create a popovercontroller
UIPopovercontroller *popOverController;
//Create an instance of the class you working in, your viewcontroller
YourViewController *popOverContent = [[YourViewController alloc] init];
//Create the popover view, you can change the size of it
UIView *popOverView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];
UISLider *slider = [[UISlider alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];
UIButton *close = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 50, 20)]
//Add slider & button to view
[popOverView addSubview:slider];
[popOverView addSubview:button];
//Add view to viewcontroller
[popOverContent addSubview:popOverView];
//Alloc & init the popovercontroller
popOverController = [[UIPopoverController alloc] initWithContentViewController:popOverContent];
//Set content size of popover
popOverController.popoverContentSize = CGSizeMake(200, 200);
//present popup from a button or frame
[popOverController presentPopoverFromRect:self.yourButton.frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
Than create an IBAction for the close button and use this line of code to dismiss the popover:
[popOverController dismissPopoverAnimated:YES];
You can find more information about popOverControllers here
Depending on what you want to do with your popup and how do you want to present it you can choose:
ViewController from storyboard if you want to present a popup in modal style and from that popup you want to show other view controllers.
Xib file for a custom UIView, if you are using the popup to display some information and get the user input after that you will dismiss the popup and the popup will be used in several places (different view controllers)
Custom UIView in one storyboard view controller, if you want to display some information and get the user input and the popup will be use only on one place (one ViewController)

How do I add a UISegmentedControl to my UIToolbar using Storyboard?

I'm trying to add a Segmented Control to my UIToolbar below, but when I try to drag it over in Storyboard it replaces my Table View.
Also, when I try to add UIBarButton to my Toolbar it pushes my Prototype Cells down... do I have my views hierarchy wrong?
When I add a UIBarButton item anyway, in the simulator the toolbar is not Black Transparent as I set in my navigation control inspector. Whats up with that?
thanks!
To create Segmented Control element in the toolbar with Interface builder it is possible to use the following hint
1) Create Segmented Control Bar Button Item in Navigation Controller:
2) Drag Bar Button Item up to Navigation Item
3) Then you should get the following structure
4) Now you can select Segmented Control to set its properties
1 - Do it programmatically. You need to assign a frame to your UIToolbar first, then you can add elements in it (the segmentedControl). I suggest though to use UIBarButtonItems, which basically are the same.
To stick it to the bottom, try to do this way:
#define SCREEN_FRAME [[UIScreen mainScreen] applicationFrame]
CGRect frame = CGRectMake(0, [[UIScreen mainScreen] bounds].size.height - 44, [[UIScreen mainScreen] bounds].size.width, 44);
UIToolbar *toolBar = [[UIToolbar alloc]initWithFrame:frame];
toolBar.frame = CGRectMake(0,self.view.frame.size.height-toolBar.frame.size.height,SCREEN_FRAME.size.width,toolBar.frame.size.height);
//Setting up the items
UIBarButtonItem *first = [[[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"image.png"] style:UIBarButtonItemStylePlain target:self action:#selector(yourAction:)] autorelease];
UIBarButtonItem *second = [[[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"image.png"] style:UIBarButtonItemStylePlain target:self action:#selector(yourAction:)] autorelease];
//Creating an array with the items
NSArray *items = [NSArray arrayWithObjects:first,second, nil];
//Assigning the array to the toolBar
[toolBar setItems: items];
Then you can set its style with
mytoolbar.barStyle = UIBarStyleBlack;
2 - Same as above, framing.
3 - You need to set your navigationController style to Black.
Use self.navigationController.navigationBar.style = UIBarStyleBlack; ,or in IB choose the buttons and from Inspector set Tint or Style to whatever you want.
Check also this question
I presume there's a navigation view controller somewhere in here. They have their own toolbar, but it's hidden and empty. The toolbar you're seeing is just a simulation of a toolbar for the purpose of letting you edit the UI.
So to do this with storyboard, don't add the segmented control to the simulated toolbar, create a new toolbar and add it to the tree of views available in your controller (as a sibling to your Table View) and create an IBOutlet to reference it. Then in your viewDidLoad method assign the items in the toolbar to the toolbar already created by the view controller.
In this example I've created a property on my view controller called (cunningly) toolbar:
#property (nonatomic, retain) IBOutlet UIToolbar toolbar;
In my view controller's implementation I assign the toolbar items manually:
-(void)viewDidAppear:(BOOL)animated
{
[self setToolbarItems:self.toolbar.items animated:NO];
[self.navigationController setToolbarHidden:NO animated:YES];
}
You'll need to hide the toolbar when the view controller is popped, for instance in the calling view controller:
-(void)viewWillAppear:(BOOL)animated
{
[self.navigationController setToolbarHidden:YES animated:YES];
}
Assuming you want to hide it, of course.
I did this by placing the UISegmentedController in a Bar Button Item.
Add a toolbar to the UINavigationController window, then enable the display of this toolbar on a ViewController screen.
Drag a bar button into the toolbar on the ViewController screen, and drag a UISegmentedController into that (resizing the button to the full width of the screen)
To wire it all up, ctrl-drag from the UISegmentedController to the corresponding .h file, drag from "referencing outlet" to the yellow view controller icon (using the same variable name), then drag from "ValueChanged" to the yellow icon (selecting the method on the controller that you want to call on a new selection).
This all seems to work as expected. (If you can't select a segment on the controller to set an initial value, you didn't connect up the referencing outlet as described above. That stumped me for a little while.)

UINavigationController barstyle property changes layout

I have a modal view controller which displays a navigation controller. The navigation controller in-turn has a regular UIViewController as its root view controller. The only UI element that the above-mentioned UIViewController has is a UISwitch.
Now here's the problem: When I change the barStyle property of the navigation controller, the layout of the UISwitch inside the UIViewController changes. Here's what I am talking about:
If I don't set the barStyle property, here's what I get:
http://img535.imageshack.us/img535/2281/plaini.png
The UISwitch is now in its 'exepected' place.
Now if I set the barStyle property,
navController.navigationBar.barStyle = UIBarStyleBlackTranslucent;
Notice that the UISwitch is behind the navigation bar:
http://img853.imageshack.us/img853/2377/blackya.png
Here's the code for the UISwitch in the UIViewController:
- (void)viewDidLoad
{
UISwitch* mySwitch = [[UISwitch alloc] initWithFrame:CGRectMake(20, 20, 100, 100)];
[self.view addSubview:mySwitch];
[mySwitch release];
}
Can someone help me understand what's happening?
The Frame shifts when you use the UIBarStyleBlackTranslucent (which is actually deprecated) property because it assumes you want your view to be underneath
The Apple Documentation says to use the following since UIBarStyleBlackTranslucent is deprecated:
navController.navigationBar.barStyle = UIBarStyleBlack;
navController.navigationBar.translucent = YES;
You could try shifting your view back in the correct place or try using the following:
navController.navigationBar.tintColor = [UIColor blackColor];
navController.navigationBar.translucent = YES;

Resources