Tab Bar controller with code or with template? - ios

I want to create an iOS app which will have use a Tab Bar controller. I know you can set this up by a template within XCode or you can just code it straight, but I'm unclear why use one approach over the other? other than I presume the template allows you to use the interface builder? if I don't want to use interface builder (and I don't) is there any point to using the tab bar app template? can I use the template but remove the interface builder bits? or if I'm doing that I might as well not use the template and just code the controller directly?

As board as the question sounds, let us look at it as a design decision and personal preference.
The Interface Builder allows you to design your layout quickly while providing you the visual bits - you know, how it would look like while designing it.
Can you achieve the same result programmatically? Yes, sure, if you can see it (where the UI element is, the color, the position it has in relation to the other elements, etc etc) while designing in your mind.
Let's say you wanted to have a UIButton, just a button, plain and have it hooked to a method called clicked when pressed. In IB you could achieve that within seconds.
Comparing that to doing it programmatically:
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self action:#selector(clicked:) forControlEvents:UIControlEventTouchUpInside];
[button setTitle:#"play a beep" forState:UIControlStateNormal];
button.frame = CGRectMake(80.0f, 80.0f, 40.0f, 40.0f);
[self.view addSubview:button];
You could do it either way - in IB or in code.
For the above case, would doing it in IB faster? Yes. But it doesn't prevent you from also doing it programmatically - it's your preference.
This serves to provide a general answer; because there are many cases involved while designing the interface as to whether one should use IB or do it programmatically.
Can you remove the IB bits? Yes.
Analogy:
Take it as buying a car and pimp it up with fog lamps, have it re-painted etc.
But if you decide to pimp your own ride by replacing every single bits on the car you buy, then why buy a car to do that and not just build one from scratch?

Related

Custom UITableViewCell Edit View

I've been looking over countless questions and answers here on SO, but could not find one that would provide the answer for my specific problem.
In a nutshell: I am trying to implement a custom edit view for my UITableViewCells. I would like to keep the basic "delete" and "rearrange/drag" actions (although on opposite ends than in the default implementation and with custom icons), and I would also like to add a "duplicate" action to them and one that navigates to a specific view (separate from the one when you click the cell in normal state). Something like this:
Ideally, I would like to trigger this edit mode on all cells when I toggle edit mode with a separate button, and on individual cells with a left swipe (without the drag option if it is not possible on an individual cell).
I have sub-classed UITableViewCell and was able to add a swipe recognizer that reveals a view ("cellOptionsContainer") with the 3 custom buttons on the right, all of which I was able to make work using tags and delegates, but that is most certainly not the right way to do things because I've added them to the cell's content view (not knowing any better):
// Add container to hold options buttons
cellOptionsContainer = [[UIView alloc] initWithFrame:CGRectMake(cellWidth,
0.0f,
(dataBase.sizeInteraction * 3),
dataBase.sizeInteraction)];
[self.contentView addSubview:cellOptionsContainer];
// Add edit button
UIButton *cellOptionsButton = [[UIButton alloc] initWithFrame:CGRectMake((0.0f),
0.0f,
dataBase.sizeInteraction,
dataBase.sizeInteraction)];
cellOptionsButton.tag = cellID;
[cellOptionsButton setImage:[UIImage imageNamed:#"iconEditOutlined"] forState:UIControlStateNormal];
[cellOptionsButton setShowsTouchWhenHighlighted:YES];
[cellOptionsButton setAdjustsImageWhenHighlighted:NO];
[cellOptionsButton addTarget:self action:#selector(presentEditView:) forControlEvents:UIControlEventTouchUpInside];
//[cellOptionsContainer addSubview:cellOptionsButton];
[cellOptionsContainer addSubview:cellOptionsButton];
Meanwhile, I was also able to make work the standard edit view in my UITableView delegate (rearranging and deleting both works fine), but I was unable to augment this standard edit view with the added functionality and visual customization illustrated above. I cannot even find a way to change the standard icons...
Any help would be much appreciated; although, I am looking for Objective-C answers as I do not know Swift! I am also doing everything programmatically; I do not use interface builder.

iOS: Creating buttons using IB vs. creating buttons programmatically

I'm a web and android developer, and now i'm learning iOS development, i noticed that if you want to build any view (let's say a button) you have to create it using Interface Builder, or you can make it happen programmatically by doing for instance :
UIButton *but= [UIButton buttonWithType:UIButtonTypeRoundedRect];
[but addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[but setFrame:CGRectMake(52, 252, 215, 40)];
[but setTitle:#"Login" forState:UIControlStateNormal];
[but setExclusiveTouch:YES];
[self.view addSubview:but];
But if you did that the button won't be visible in the Storyboard.
In android there are XML files where you can build your layouts whether with drag and drop or with xml programming(which i prefer) and both ways the button will be visible if you want to look at the layout.
But here in iOS i can't find a way to look at the Storyboard code.
I think what i'm asking is: is it possible to see the Storyboard code ? and what is the best approach for designing views in iOS ?
NOTE i researched a lot, but i'm new to iOS world so i found a lot of stuff that i don't recognize or understand yet.
Right click on Main.storyboard then open it as Source code.
This blog post has an interesting discussion on how to decide whether to use Storyboards or XIBs to create views, or to create them programatically. Personally, I prefer using XIBs to set up the main layout as I can easily drag and drop the views and add constraints, and I have a more visual overview of my view controller. I then always have the option of making further changes programatically.
But then again, I think it's personal preference, so there's no right way.
You're right, UI stuff made in code does not (unfortunately) reflect in the interface builder.
Whether or not to use the interface builder, do all UI "by hand" or a combination is a matter of personal preferences.
I personally prefer to do UI stuff in code unless I'm prototyping. This gives me more fine grained handling and I'm able to have my UI logic contained in one place.

Is it possible to add one UIButton object to multiple UIImageViews?

I have a scrollView and inside this scrollView, there are various PFImageView objects.
I need a single UIButton object on every PFImageView (same place and same button) that will load a subView with the information of the image tapped.
Is this possible?
So, let me get this straight... You want to create only one button, place it on multiple imageViews and have it respond differently (depending on which imageView was tapped)?
In this case...
No, you can't have 1 button in N places.
But... you can create N button objects and have them all assigned to 1 single target method in which... you can have different functionality based on the button's tag.
PS: UICollectionView would be cleaner approach.
You can't have a single control in multiple locations, it's that simple.
I saw your comment about not wanting to use a UICollectionView because it's not the design you're looking for, but I wonder what kind of design you want to create with UIScrollView only because UICollectionView doesn't works.
UICollectionView is not restrained to line/grid display of cells, but can be customized in a huge amounts of ways. You should watch the WWDC 2012 video "Introducing Collection Views" : at the end of it, some UICollectionView possibilities are shown and some of them are quite impressive.
Also, https://www.cocoacontrols.com/ website is a good place to find custom controls and even inspiration for your own design.
This is a little digression from the original issue, but I really think that a collection of UIImageView within an UIScrollView can be created with a UICollectionView, especially if you want to add some controls within your "cells".
Yes. You should use specific control events when adding actions to the button.
E.g. in my application i record/stop/cancel sound as long as user hold a button (drag out to cancel).. like a walkie talkie phone...
[recordButton addTarget:self action:#selector(recordStart) forControlEvents:UIControlEventTouchDown];
[recordButton addTarget:self action:#selector(recordStop) forControlEvents:UIControlEventTouchUpInside];
[recordButton addTarget:self action:#selector(recordCancel) forControlEvents:UIControlEventTouchDragExit];

how to implement toggle behavior using Imageview, no UIButton

I have to implement a toggle like behavior between to differentiate between two states, client doesn't want UIbuttons because he has his own png's and wants them to be used.
so I need to have to UIImageViews next to each other.
I am talking about UIImageviews because the toggle behavior the client wants should be a sliding one, as in the user has two options in the control ( like on / off ) he slides the UI from on to off, or off to on, I was given 4 png's: 2 for the on state ( normal and depressed ) and 2 for the off state ( Normal and depressed ) .
I also looked into UISwitch, but again to customize it the way I want to seemed cumbersome, any ideas?
You can achieve what your client wants this way:
UIButton *nameOfButton = [UIButton buttonWithType:UIButtonTypeCustom];
[nameOfButton setImage:[UIImage imageNamed:nameOfYourClientsPNGFiles] forState:UIControlStateNormal];
[nameOfButton addTarget:self action:#selector(actionForButton) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:nameOfButton];
//Note that all the words are to be replaced with what your methods and pointers are.
Also if your clients PNG files are not the traditional square images I strongly recommend looking into this Custom Class:-)
OBShapedButton
It sorts out the problem that the user can only tab and click the shape of the image.

Is there a way to create a UIButton-like frame in iOS without the button?

I'm working on an iPad app right now that takes as input an XML file and outputs a Cocoa-like user interface that's dynamically generated based on the content of the XML file. Part of the specification that I've been given dictates that one of the UI elements is a simple frame that looks exactly like a Cocoa Touch button, but does not act as a button; IE, it does nothing when clicked on, and does not visually change in any way. I wanted to use the iOS equivalent of a NSBox mocked up to look like a NSButton, but upon inspection it seems that there is no UIBox class that is equivalent to a NSBox the same way that UIButton is similar/equivalent to the NSButton. Can anyone more experienced in iOS than I suggest a design pattern for creating a button's 'frame' without the additional weight of a new button?
Simple, just disable user interaction on the button. This way it will still look like a button but it won't act like one.
[myButton setUserInterActionEnabled:NO];
Alternatively you could create a UIImageView and use Quartz to adjust the corner radius of the image.
#import QuartzCore/QuartzCore.h
[[myImageView layer] setCornerRadius:15.0];

Resources