archive-com.com » COM » T » TECHOTOPIA.COM

Total: 1018

Choose link from "Titles, links and description words view":

Or switch to "Titles and links view".
  • Working with iPad iOS 6 Auto Layout Constraints in Interface Builder - Techotopia
    be the case In recognition of this fact Interface Builder provides a wide range of options and visual cues that are designed to ease the task of creating auto layout constraints The Auto Layout Features of Interface Builder A number of features have been added to Interface Builder in order to assist in the implementation of auto layout based constraints This section will present a guided tour of these features Automatic Constraints As is evident from the previous example Interface Builder will automatically set constraints on views as they are added to a layout and will continue to add and remove constraints as necessary as changes are made to the layout during the design process It is important to understand that there is a distinction between constraints added automatically by Interface Builder and those added manually by the developer In fact constraints added manually are referred to as user constraints As will be demonstrated later in this chapter constraints added by Interface Builder may be removed by first promoting them to be user constraints User constraints may also be configured for standard spacing as will be demonstrated later in this chapter The way that a view is positioned on the layout will also dictate how an Interface Builder constraint is configured Moving the view to the center of the parent view will cause center guidelines to appear Dropping the view at that point will result in a center constraint being created Moving a view close to the edge of the window view will result in a margin guideline appearing as illustrated in Figure 18 10 If placed at that location Interface Builder will implement a constraint that fixes the corresponding edge of the view from the edge of the display using the Apple recommended standard spacing distance Figure 18 5 The same standard setting will also be implemented if one view is moved close to the edge of another view as indicated by the vertical guide that appears on the edge of the view approaching the second view This guide is visible on the right hand edge of the Save button in Figure 10 6 as it is moved close to the Cancel button Figure 18 6 Visual Cues Interface Builder includes a number of visual cues in the layout canvas to highlight the constraints currently configured on a view When a view is selected within the Interface Builder layout canvas the constraints that reference that view will be represented visually Consider for example the label view created in our AutoLayout example application When selected in the canvas a number of additional lines appear as shown in Figure 18 7 Figure 18 7 The vertical line that runs through the center of the label indicates the presence of a constraint that positions the label in the horizontal center of the parent view analogous to the NSLayoutAttributeCenterX attribute If expressed as an equation therefore this would read as label NSLayoutAttributeCenterX superview NSLayoutAttributeCenterX The I beam line running from the bottom edge

    Original URL path: http://www.techotopia.com/index.php/Working_with_iPad_iOS_6_Auto_Layout_Constraints_in_Interface_Builder (2016-02-13)
    Open archived version from archive


  • An iPad iOS 6 Auto Layout Example - Techotopia
    to make the user interface appear correctly in both orientations The first step is to consider the constraints that are currently set on the label and text views The text view has a constraint that forces it to be centered horizontally in the parent view The label on the other hand has a constraint that connects its trailing edge to the leading edge of the text view using the standard default spacing The absence of a constraint on the left hand edge of the label is resulting in the label being pushed off the screen in portrait mode One possible solution to this problem is to create a new constraint on the label that puts some space between the left hand edge of the label and the left hand edge of the parent view To add this constraint rotate the view back to landscape so that the label is fully visible select the label followed by the Editor Pin Leading Space to Superview menu option Now rotate to Portrait mode and note that the label is unfortunately now compressed Figure 19 6 The reason for this problem is that the Text View contains a constraint which forces it to be centered horizontally within the super view with a priority of 1000 Similarly the label has constraints that dictate that the leading and trailing edges of the label must be the standard width from the superview and text view respectively Since these also have a priority of 1000 the system has no choice but to compress the label in order to satisfy the constraints In order for the label to be fully visible one of these priorities needs to be reduced We already know from experience that without the constraint on the leading edge of the label the left hand edge will be clipped by the superview window when the device is in portrait orientation Another option is to lower the priority on the space constraint between the label and text view With this in mind select the constraint and in the Attribute Inspector slowly move the Priority slider to the left Note that as the slider moves a panel appears Figure 19 7 that describes the implication of the currently selected priority value When seeking specific layout behavior it is important to make sure that the correct priority is selected Figure 19 7 Using the slider select a priority value close to 500 Check the objects in the view in portrait orientation and note that they are now overlapping Clearly the space between the labels needs to be a required constraint to prevent overlap Select the constraint once again therefore and return the priority value to 1000 The only remaining constraint to experiment with is the horizontal center constraint on the Text View Select the Text View object and display the Size Inspector in the Utilities panel Under the Constraints section select the option to Select and Edit the Align Center X to superview constraint depending on how the layout

    Original URL path: http://www.techotopia.com/index.php/An_iPad_iOS_6_Auto_Layout_Example (2016-02-13)
    Open archived version from archive

  • Implementing iPad iOS 6 Auto Layout Constraints in Code - Techotopia
    200 Note that since this constraint is not related to another view the toItem argument is set to nil Adding a Constraint to a View Once a constraint has been created it needs to be assigned to a view in order to become active This is achieved by passing it through as an argument to the addConstraint method of the view instance to which it is being added In the case of multiple constraints each is added by a separate call to the addConstraint method This leads to the question of how to decide which view the constraint should be added to In the case of a constraint that references a single view the constraint must be added to the immediate parent of the view When a constraint references two views the constraint must be applied to the closest ancestor of the two views Consider for the purposes of an example the view hierarchy illustrated in Figure 20 1 Figure 20 1 A constraint referencing only Label A should be added to the immediate parent in this case View B A constraint referencing Button B and Label B on the other hand must be added to the nearest common ancestor which in this case is View C A constraint referencing Button A and Button B must once again be added to the nearest common ancestor which equates to View A For the purposes of an example the following code excerpt creates a new constraint and adds it to a view NSLayoutConstraint myConstraint NSLayoutConstraint constraintWithItem mybutton attribute NSLayoutAttributeWidth relatedBy NSLayoutRelationEqual toItem mylabel attribute NSLayoutAttributeWidth multiplier 5 constant 0 superview addConstraint myConstraint Turning off Auto Resizing Translation When adding views to a layout in code the toolkit will by default attempt to convert the autosizing mask for that view to auto layout constraints Unfortunately those auto generated constraints will conflict with any constraints added within the application code It is essential therefore that translation be turned off for views to which constraints are to be added This is achieved by calling the setTranslatesAutoresizingMaskIntoConstraints method of the target view passing through NO as an argument For example the following code creates a new Button view turns off translation and then adds it to the parent view UIButton mybutton UIButton buttonWithType UIButtonTypeRoundedRect mybutton setTitle My Button forState UIControlStateNormal mybutton setTranslatesAutoresizingMaskIntoConstraints NO superview addSubview mybutton An Example Application Create a new Xcode project for the iPad using the Single View Application template Enter AutoLayoutCode as the product name and class prefix making sure that the storyboard and automatic reference counting options are both selected Creating the Views For the purpose of this example the code to create the views and constraints will be added to the viewDidLoad method of the AutoLayoutCode view controller Select the AutoLayoutCodeViewController m file locate this method and modify it to create a button and a label and add them to the main view void viewDidLoad super viewDidLoad UIView superview self view UILabel mylabel UILabel alloc init mylabel setTranslatesAutoresizingMaskIntoConstraints NO mylabel

    Original URL path: http://www.techotopia.com/index.php/Implementing_iPad_iOS_6_Auto_Layout_Constraints_in_Code (2016-02-13)
    Open archived version from archive

  • Implementing iPad Cross-Hierarchy Auto Layout Constraints in iOS 6 - Techotopia
    off center in the horizontal axis Figure 21 2 Begin by launching Xcode and selecting the options to create a new iPad iOS application based on the Single View Application template Enter CrossView as the product name and class prefix set the device to iPad and select the Use Storyboards and Use Automatic Reference Counting options if they are not already selected Select the MainStoryboard storyboard file from the project navigator panel and drag and drop UIView Button and Text Field views onto the design canvas as illustrated in Figure 21 2 making sure to center the text field object horizontally and vertically within the parent view Establishing Outlets In order to set a cross hierarchy constraint within code it will be necessary to implement some outlets Since the constraint will need to reference both the button and the text view outlets need to be configured for these views Select the Text Field object and display the Assistant Editor using View Assistant Editor Show Assistant Editor menu option or by selecting the center button the one containing an image of a bow tie and tuxedo of the row of Editor toolbar buttons in the top right hand corner of the main Xcode window Ctrl click on the Text Field object in the view and drag the resulting line to the area immediately beneath the interface directive in the Assistant Editor panel Upon releasing the line the configuration panel will appear Configure the connection as an Outlet named myTextView and click on the Connect button Repeat the above steps to add an outlet for the button object named myButton As currently constrained the text field object is centered horizontally and vertically within the view we are referring to as View B In place of this constraint we need the text field to be aligned with the center of the button object This will involve removing the CenterX constraint and replacing it with a new constraint referencing the button This requires outlets for both the View B instance and the CenterX constraint Ctrl click on the View B parent of the text field object and drag the resulting line to the area immediately beneath the interface directive in the Assistant Editor Release the line and configure an outlet named viewB Next with the text field selected locate the CenterX Alignment constraint in the Document Outline panel Figure 21 3 and create a new outlet for this object named centerConstraint Figure 21 3 Writing the Code to Remove the Old Constraint With the necessary outlets created the next step is to write some code to remove the center constraint from the text field object For the purposes of this example all code will be added to the viewDidLoad method of the view controller Select the CrossViewViewController m file and locate and modify the method as follows void viewDidLoad super viewDidLoad viewB removeConstraint centerConstraint All that the code is doing is calling the removeConstaint method of view B using the previously configured outlet and

    Original URL path: http://www.techotopia.com/index.php/Implementing_iPad_Cross-Hierarchy_Auto_Layout_Constraints_in_iOS_6 (2016-02-13)
    Open archived version from archive

  • Understanding the iPad iOS 6 Auto Layout Visual Format Language - Techotopia
    a horizontal spacer between two view objects is represented by a hyphen mybutton1 mybutton2 The above example instructs the auto layout system to create a constraint using the standard spacing for views The following construct on the other hand specifies a spacing distance of 30 points between the two views mybutton1 30 mybutton2 By default constraints of the type outlined above are assumed to be horizontal constraints Vertical constraints are declared using a V prefix For example the following syntax establishes a vertical spacing constraint between two views V mylabel 50 mybutton For consistency and completeness horizontal constraints may optionally be prefixed with H The width of a view can be set specifically as follows mybutton 100 Alternatively inequality can be used mybutton 100 Using similar syntax the width of one view can be constrained to match that of a second view mylabel mybutton2 When using the visual format language the superview of the view for which the constraint is being described is represented by the character For example the following visual format language construct declares a constraint for the mybutton1 view that attaches the leading and trailing edges of the view to the left and right edges of the containing superview with a spacing of 20 and 30 points respectively 20 mybutton1 30 The language also allows priorities to be declared The following excerpt specifies that the width of mybutton1 must be greater than or equal to 70 points with a priority value of 500 mybutton1 70 500 Of particular importance however is the fact that the language may be used to construct multiple constraints in a single sequence for example V 20 mybutton1 70 500 mybutton2 mybutton1 30 mybutton3 Using the constraintsWithVisualFormat Method As previously described visual language format based constraints are created via a call to the constraintsWithVisualFormat method of the NSLayoutConstraint class There are however a number of other arguments that the method is able to accept The syntax the method is as follows NSLayoutConstraint constraintsWithVisualFormat visual format string options options metrics metrics views views dictionary The visual format string is of course the visual format language string that describes the constraints that are to be created The options are required to be set when the constraint string references more than one view The purpose of this is to indicate how the views are to be aligned and the value must be of type NSLayoutFormatOptions for example NSLayoutFormatAlignAllLeft NSLayoutFormatAlignAllRight NSLayoutFormatAlignAllTop NSLayoutFormatAlignAllBaselines etc The metrics argument is an optional NSDictionary object containing the corresponding values for any constants referenced in the format string Finally the views dictionary is an NSDictionary object that contains the view objects that match the view names referenced in the format string The iOS 6 SDK provides a convenience macro that can be used to construct this dictionary for the objects referenced in the format string for example NSDictionary viewsDictionary NSDictionaryOfVariableBindings mybutton1 mybutton2 When using a visual format string that will result in the creation of multiple constraints the options should include an

    Original URL path: http://www.techotopia.com/index.php/Understanding_the_iPad_iOS_6_Auto_Layout_Visual_Format_Language (2016-02-13)
    Open archived version from archive

  • Using Xcode Storyboards to create an iOS 6 iPad Tab Bar Application - Techotopia
    been created Renaming the Initial View Controller The next step in creating the application involves renaming the TabBarViewController class provided by Xcode to a more descriptive name Since this view controller presents the view that will be displayed when the user touches the first tab on the tab bar we will name this class Tab1ViewController To achieve this select the TabBarViewController h file and use the mouse to highlight the TabBarViewController class name in the editing panel With the name highlighted select the Edit Refactor Rename menu option In the resulting panel enter Tab1ViewController into the text box and click on Preview In the preview panel select the Save button followed by the option to Disable snapshots Adding the View Controller for the Second Content View The ultimate goal of this chapter is to create a tab bar based application consisting of two tabs with corresponding views Xcode has already created the first view controller for us and we have subsequently renamed this class The next step therefore is to add the view controller for the second view To achieve this select the File New File menu option and on the resulting panel select Objective C class Click Next and on the next screen name the new class Tab2ViewController and change the Subclass of menu to UIViewController Verify that the Targeted for iPad option is selected and the option to generate an XIB file is off before clicking Next to proceed with the creation process Adding the Tab Bar Controller to the Storyboard As previously explained the navigation between view controllers in a Tab Bar based interface is handled by a Tab Bar Controller It will be necessary therefore to add one of these to our storyboard Begin by selecting the MainStoryboard storyboard file in the Xcode project navigator panel and note as illustrated in Figure 24 1 that the storyboard currently only contains the original view controller instance added for us by Xcode when the project was created Figure 24 1 In order to add a Tab Bar Controller to the storyboard select the Tab1 View Controller in the storyboard design area and select the Editor Embed In Tab Bar Controller menu option The Tab Bar Controller will subsequently appear in the storyboard already connected to the Tab Bar Controller as shown in Figure 24 2 Figure 24 2 Now that the Tab Bar Controller has been added the next task is to add a view controller to the storyboard that will correspond to the Tab2ViewController class previously added to the project Adding a Second View Controller to the Storyboard The second view controller may be added to the storyboard simply by dragging and dropping one from the Object Library panel View Utilities Show Object Library Once the new view controller has been added to the storyboard canvas select it so that it is highlighted with a blue border and then display the Identity Inspector panel View Utilities Show Identity Inspector Within the inspector panel change the Class setting

    Original URL path: http://www.techotopia.com/index.php/Using_Xcode_Storyboards_to_create_an_iOS_6_iPad_Tab_Bar_Application (2016-02-13)
    Open archived version from archive

  • An Overview of iPad iOS 6 Table Views and Xcode Storyboards - Techotopia
    Dynamic tables also known as prototype based tables on the other hand are intended for use when a variable number of rows need to be displayed from a data source Within the storyboard editor Xcode allows you to visually design a prototype table cell which will then be replicated in the dynamic table view at runtime in order to display data to the user The Table View Delegate and dataSource Each table view in an application needs to have a delegate and a dataSource associated with it with the exception of static tables which do not have data source The dataSource implements the UITableViewDataSource protocol which basically consists of a number of methods that define title information how many rows of data are to be displayed how the data is divided into different sections and most importantly supplies the table view with the cell objects to be displayed The delegate implements the UITableViewDelegate protocol and provides additional control over the appearance and functionality of the table view including detecting when a user touches a specific row defining custom row heights and indentations and also implementation of row deletion and editing functions Table View Styles Table views may be configured to use either plain or grouped style In the grouped style the rows are grouped together in sections represented by rounded rectangles For example Figure 25 1 shows a table view configured to use the grouped style Figure 25 1 In the case of the plain style the items are listed without separation and using the full width of the display Figure 25 2 Table Views using plain style can also be indexed whereby rows are organized into groups according to specified criteria such as alphabetical or numerical sorting Table View Cell Styles In addition to the style of the Table View itself different styles may also be specified for the individual table cells unless custom table cells are being used The iOS 6 SDK currently supports four different cell styles UITableViewCellStyleDefault only the labelText in black and left aligned UITableViewCellStyleSubtitle labelText in black and left aligned with the detailLabeltext positioned beneath it in a smaller font using a gray foreground UITableViewCellStyleValue1 labelText in black left aligned and the smaller detailLabelText in blue on the same line and right aligned UITableViewCellStyleValue2 labelText in blue on left side of cell right aligned and detailedLabelText on right of cell left aligned and black Table View Cell Reuse A table view is at the basic level comprised of a UITableView object and a UITableViewCell for each row to be displayed When developing using iOS releases prior to iOS 6 it was the responsibility of the developer to write code to create instances of the cells as required by the table view object In iOS 6 this has changed The code for a typical iOS 6 application using a table view will not directly create instances of a cell The reasoning behind this becomes evident when performance and memory requirements are taken into consideration Consider

    Original URL path: http://www.techotopia.com/index.php/An_Overview_of_iPad_iOS_6_Table_Views_and_Xcode_Storyboards (2016-02-13)
    Open archived version from archive

  • Using Xcode Storyboards to Build Dynamic iOS 6 iPad TableViews with Prototype Table View Cells - Techotopia
    to visually construct the user interface elements that are to appear in the table cells and then replicate that cell at runtime For the purposes of this example each table cell needs to display an image view and two labels which in turn will be connected to outlets that we will later declare in the CarTableViewCell subclass Using Interface Builder components may be dragged from the Object Library panel and dropped onto a scene within the storyboard Note however that this is only possible when the storyboard view is zoomed in With this in mind verify that the storyboard is zoomed in using the controls in the bottom right hand corner of the canvas and then drag and drop two Labels and an Image View object onto the prototype table cell Resize and position the items so that the cell layout resembles that illustrated in Figure 26 4 making sure to stretch the label objects so that they extend toward the right hand edge of the cell Note also that the height of the cell itself has been increased Figure 26 4 Having configured the storyboard elements for the table view portion of the application it is time to begin modifying the table view and cell subclasses Modifying the CarTableViewCell Class Within the storyboard file two labels and an image view were added to the prototype cell which in turn has been declared as an instance of our new CarTableViewCell class In order to manipulate these user interface objects from within our code we need to establish three outlets connected to the objects in the storyboard scene Begin therefore by selecting the image view object displaying the Assistant Editor and making sure that it is displaying the content of the CarTableViewCell h file If it is not use the bar across the top of the editor panel to select this file Ctrl click on the image view object in the prototype table cell and drag the resulting line to a point just below the interface line in the Assistant Editor window Release the line and use the connection panel to establish an outlet named carImage Figure 26 5 Repeat these steps to establish outlets for the two labels named makeLabel and modelLabel respectively Once the connections are in place the CarTableViewCell h file should read as follows import UIKit UIKit h interface CarTableViewCell UITableViewCell property nonatomic strong IBOutlet UIImageView carImage property nonatomic strong IBOutlet UILabel makeLabel property nonatomic strong IBOutlet UILabel modelLabel end Creating the Table View Datasource Dynamic Table Views require a datasource to provide the data that will be displayed to the user within the cells By default Xcode has designated the CarTableViewController class as the datasource for the table view controller in the storyboard It is within this class therefore that we can build a very simple data model for our application consisting of a number of arrays The first step is to declare these as properties in the CarTableViewController h file import UIKit UIKit h interface

    Original URL path: http://www.techotopia.com/index.php/Using_Xcode_Storyboards_to_Build_Dynamic_iOS_6_iPad_TableViews_with_Prototype_Table_View_Cells (2016-02-13)
    Open archived version from archive



  •