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".
  • An Introduction to Auto Layout in iOS 9 - Techotopia
    800 The label is now permitted to grow in width up to the specified limit allowing longer content to be displayed without clipping Auto layout constraints are by nature interdependent and as such situations can arise where a constraint on one view competes with a constraint on another view to which it is connected In such situations it may be necessary to make one constraint stronger and the other weaker in order to provide the system with a way of arriving at a layout solution This is achieved by assigning priorities to constraints Priorities are assigned on a scale of 0 to 1000 with 1000 representing a required constraint and lower numbers equating to optional constraints When faced with a decision between the needs of a required constraint and an optional constraint the system will meet the needs of the required constraint exactly while attempting to get as close as possible to those of the optional constraint In the case of two optional constraints the needs of the constraint with the higher priority will be addressed before those of the lower Alignment Rects When working with constraints it is important to be aware that constraints operate on the content of a view not the frame in which a view is displayed This content is referred to as the alignment rect of the view Alignment constraints such as those that cause the center of one view to align with that of another will do so based on the alignment rects of the views disregarding any padding that may have been configured for the frame of the view Intrinsic Content Size Some views also have what is known as an intrinsic content size This is the preferred size that a view itself believes it needs to be to display its content to the user A Button view for example will have an intrinsic content size in terms of height and width that is based primarily on the text or image it is required to display and internal rules on the margins that should be placed around that content When a view has an intrinsic content size Auto Layout will automatically assign two constraints for each dimension for which the view has indicated an intrinsic content size preference i e height and or width One constraint is intended to prevent the size of the view becoming larger than the size of the content otherwise known as the content hugging constraint The other constraint is intended to prevent the view from being sized smaller than the content referred to as the compression resistance constraint eBookFrenzy com Purchase the full edition of this iOS 9 App Development Essentials book in eBook 12 99 or Print 39 99 format iOS 9 App Development Essentials Print and eBook ePub PDF Kindle edition contains 105 chapters Content Hugging and Compression Resistance Priorities The resize behavior of a view with an intrinsic content size can be controlled by specifying compression resistance and content hugging priorities A view with a

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


  • Working with iOS 9 Auto Layout Constraints in Interface Builder - Techotopia
    of the Align menu With the label still selected in the view canvas and the Align menu panel displayed enable the checkbox next to the Horizontally in Container property Figure 19 12 Since no offset from the center is required leave the offset value at 0 Figure 19 12 With the constraint appropriately configured click on the Add 1 Constraint button to add the constraint to the view Having configured some constraints rotate the orientation once again noting this time that the label is visible and positioned sensibly Testing different display form factors in the Preview screen should also demonstrate that the constraints are working to keep the label correctly positioned for different devices Figure 19 13 In this example so far only a small subset of the Auto Layout features provided by Xcode 7 has been used In actual fact Xcode 7 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 are provided in Xcode 7 in order to assist in the implementation of Auto Layout based constraints This section will present a guided tour of many of these features Suggested Constraints When objects are added to a layout canvas Interface Builder does not implement any default constraints on those views leaving the developer to add constraints as needed There is however the option to have Interface Builder apply suggested constraints When this option is used Interface Builder will apply what it believes to be the correct constraints for the layout based on the positioning of the views Suggested constraints can be added either to the currently selected view objects or to an entire scene layout In situations where constraints are missing from a layout resulting in warnings Interface Builder also provides the option to automatically add the constraints that it believes are missing The options to perform these tasks are accessed via the Resolve Auto Layout Issues menu in the toolbar as illustrated in Figure 19 14 eBookFrenzy com Purchase the full edition of this iOS 9 App Development Essentials book in eBook 12 99 or Print 39 99 format iOS 9 App Development Essentials Print and eBook ePub PDF Kindle edition contains 105 chapters The top section of the menu represents tasks that relate to the currently selected views in the canvas whilst the options in the lower section apply to all views in the currently selected view controller scene Figure 19 14 Most of the time the suggested constraints will exactly match the required layout behavior and occasionally the suggested constraints will be incorrect Most of the time however the suggested constraints provide an excellent starting point for implementing Auto Layout A typical process for designing a user interface might therefore involve positioning the views by dragging and dropping them into place applying suggested constraints and then editing and fine tuning those constraints to perfect the layout To see suggested constraints in action select the label view in the AutoLayoutExample project and select the Clear Constraints option from the Resolve Auto Layout Issues menu At this point there are no constraints in the layout and the old positioning problem appears when the view is rotated With the label still selected choose the Reset to Suggested Constraints menu option listed under Selected Views A review of the view canvas and change of orientation should demonstrate that Interface Builder has suggested and applied the exact same constraints that we previously added manually Visual Cues Interface Builder includes a number of visual cues in the layout canvas to highlight the constraints currently configured on a view and to draw attention to areas where problems exist When a view is selected within the layout canvas the constraints that reference that view will be represented visually Consider for example the label view created in our AutoLayoutExample application When selected in the canvas a number of additional lines appear as shown in Figure 19 15 Figure 19 15 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 NSLayoutAttribute CenterX attribute If expressed as an equation therefore this would read as label NSLayoutAttribute CenterX superview NSLayoutAttribute CenterX The I beam line running from the bottom edge of the label view to the bottom edge of the parent view indicates that a vertical space constraint is in place between the two views The absence of any additional visual information on the line indicates that this is an equality constraint Figure 19 16 shows an example of a greater than or equal to horizontal constraint between two button views Figure 19 16 The horizontal line running beneath the Button label text indicates that constraints are in place to horizontally align the content baseline represented by NSLayoutAttributeBaseline of the two buttons Width constraints are indicated by an I beam line running parallel to the edge of the view in the corresponding dimension The text view object in Figure 19 17 for example has a greater than or equal to width constraint configured Figure 19 17 Highlighting Constraint Problems Interface Builder also uses a range of visual cues and decorations to indicate that constraints are either missing ambiguous or in conflict Valid and complete Auto Layout configurations are drawn using blue lines When part of a layout is ambiguous the constraint lines are orange Ambiguity typically occurs when a constraint is missing Take for example the label view used earlier in the chapter If only the horizontal center constraint is set that constraint line will appear in orange because Auto Layout does not know where to position the view in the vertical plane Once the second constraint is set between the bottom edge of the label and the bottom of the superview the constraint line will turn blue to indicate that the layout is no longer ambiguous Red constraint lines are

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

  • An iOS 9 Auto Layout Example - Techotopia
    Constraint button Figure 20 5 With the label still selected use the Align menu to constrain the label to the horizontal center of the container All that now remains is to establish some constraints between the two labels Ctrl click on the left most label and drag the line to the right hand label before releasing the line From the resulting menu hold down the Shift key on the keyboard and select the Baseline and Horizontal Spacing options to align the content baselines of the two labels and to add a spacing constraint With both options selected hit the keyboard return key to add the constraints Rotate the canvas display into landscape mode by selecting the view controller and clicking on the left most icon in the layout scene toolbar so that the view is highlighted in blue Display the Attributes Inspector in the Utilities panel and change the Orientation value under Simulated Metrics to Landscape Note that the layout now re organizes to accommodate the orientation change Figure 20 6 If the layout does not match that shown above it may be necessary to update the frames to adopt the new constraint settings using the Update Frames option listed under the All Views in Container heading in the Resolve Auto Layout Issues menu Adjusting Constraint Priorities Up until this point the layout is behaving correctly using basic constraints with default priorities We are now going to introduce some problems that cannot be handled adequately by the constraints as they currently stand With the view still in landscape mode double click on the left hand label and change the text so that it reads Customer Record Using the Attributes Inspector change the Alignment property so that the text is right aligned With the view in landscape mode the label appears correctly positioned Rotate the view to portrait orientation however and the label is clearly being clipped by the left hand edge of the parent view Figure 20 7 Clearly there is some work to be done 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 views The right hand label has a constraint that forces it to be centered horizontally in the parent view The left hand label on the other hand has a constraint that connects its trailing edge to the leading edge of the second label The absence of a constraint on the left hand edge of the Customer Record label is resulting in the label being pushed off the screen in portrait mode One possible solution to this problem might be to create a new constraint on the Customer Record label that puts some space between the left hand edge of the customer record 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 Customer Record label display the Pin menu turn

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

  • Implementing iOS 9 Auto Layout Constraints in Code - Techotopia
    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 21 1 Figure 21 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 var myConstraint NSLayoutConstraint item myButton attribute NSLayoutAttribute Width relatedBy NSLayoutRelation Equal toItem myLabel attribute NSLayoutAttribute Width multiplier 5 0 constant 0 self view addConstraint myConstraint eBookFrenzy com Purchase the full edition of this iOS 9 App Development Essentials book in eBook 12 99 or Print 39 99 format iOS 9 App Development Essentials Print and eBook ePub PDF Kindle edition contains 105 chapters 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 in code This is achieved by setting the setTranslatesAutoresizingMaskIntoConstraints property of the target view to false For example the following code creates a new Button view turns off translation and then adds it to the parent view let myButton UIButton myButton setTitle My Button forState UIControlState Normal myButton translatesAutoresizingMaskIntoConstraints false self view addSubview myButton An Example Application Create a new Xcode project using the Single View Application template Select Universal from the Devices menu and Swift from the language menu entering AutoLayoutCode as the product name 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 ViewController swift file locate this method and modify it to create a button and a label and add them to the main view override func viewDidLoad super

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

  • Implementing Cross-Hierarchy Auto Layout Constraints in iOS 9 - Techotopia
    panel select the view and change the background color to a light shade of grey using the Attributes Inspector Drag and drop UIView Button and Label views onto the design canvas as illustrated in Figure 22 2 making sure to center the label object horizontally and vertically within the parent view Select the newly added view object click on the Resolve Auto Layout Issues menu from the toolbar in the lower right hand corner of the canvas and select the Reset to Suggested Constraints option listed under All Views in View Controller eBookFrenzy com Purchase the full edition of this iOS 9 App Development Essentials book in eBook 12 99 or Print 39 99 format iOS 9 App Development Essentials Print and eBook ePub PDF Kindle edition contains 105 chapters 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 label outlets need to be configured for these views Select the label object and display the Assistant Editor using the View Assistant Editor Show Assistant Editor menu option or by selecting the center button the one containing an image of two interlocking circles of the row of Editor toolbar buttons in the top right hand corner of the main Xcode window Make sure that the Assistant Editor is showing the ViewController swift file Ctrl click on the Label object in the view and drag the resulting line to the area immediately beneath the class declaration directive in the Assistant Editor panel Upon releasing the line the connection panel will appear Configure the connection as an Outlet named myLabel and click on the Connect button Repeat the above steps to add an outlet for the button object named myButton As currently constrained the label object is centered horizontally within the view we are referring to as View B In place of this constraint we need the label 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 label object and drag the resulting line to the area immediately beneath the previously declared outlets in the Assistant Editor Release the line and configure an outlet named viewB Next select the label object so that the associated constraint lines appear Click on the vertical line passing through the label view so that it highlights Ctrl click on the constraint line and drag to the Assistant Editor panel Figure 22 3 and create a new outlet for this object named centerConstraint Figure 22 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 label object For the purposes of this example all code will

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

  • Understanding the iOS 9 Auto Layout Visual Format Language - Techotopia
    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 eBookFrenzy com Purchase the full edition of this iOS 9 App Development Essentials book in eBook 12 99 or Print 39 99 format iOS 9 App Development Essentials Print and eBook ePub PDF Kindle edition contains 105 chapters 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 for 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 AlignAllLeft AlignAllRight AlignAllTop AlignAllBaselines 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 When using a visual format string that will result in the creation of multiple constraints the

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

  • Using Size Classes to Design Adaptable Universal iOS 9 User Interfaces - Techotopia
    the view controller layout canvas in the storyboard will change size to represent an iPhone screen in portrait orientation Once a size class has been selected layout changes made to the storyboard will apply only to that class Note also that the size class indicator now changes to wCompact hRegular Figure 24 5 reflecting the currently selected size class It is also worth noting that the status bar has changed to a blue background This is intended as a subtle reminder that the layout is no longer in the wAny hAny mode and that layout changes will not apply to all size classes Figure 24 5 A Universal User Interface Tutorial The remainder of this chapter will work through the creation of a universal layout example Create a new Xcode Single View Application project named UniversalDemo with the language option set to Swift and the Devices menu set to Universal Once the project has been created select the Main storyboard file display the File Inspector in the Utilities panel and make sure that both the Use Auto Layout and Use Size Classes options are enabled Designing the iPad Layout The iPad layout will serve as the base configuration for the user interface so begin by making sure that the size class settings for Interface Builder are currently set to wAny hAny Add an ImageView and two Buttons to the view controller layout so that the user interface layout resembles that of Figure 24 6 Edit the text on the button views to read Send and Edit respectively Figure 24 6 Select the Image View instance in the layout and use the Pin menu to set constraints on the top and left edges of the view of 0 with the Constrain to margins option enabled as shown in Figure 24 7 and click on the Add 2 Constraints button Figure 24 7 eBookFrenzy com Purchase the full edition of this iOS 9 App Development Essentials book in eBook 12 99 or Print 39 99 format iOS 9 App Development Essentials Print and eBook ePub PDF Kindle edition contains 105 chapters Ctrl click and drag diagonally across the Image View as shown in Figure 24 8 On releasing the line select Aspect Ratio from the resulting menu Figure 24 8 With the Image View still selected display the Pin menu a second time and enable the right hand edge spacing to nearest neighbor constraint Click on the drop down menu for the constraint value and select the View option Figure 24 9 so that the constraint is relative to the parent view and not one of the Button views Click on the Add 1 Constraint button to commit the change Figure 24 9 With the Image View still selected Shift click on the Edit button so that both views are selected Display the Auto Layout Align menu enable the Vertical Centers constraint option and add the constraint Select just the Edit button and using the Pin menu configure spacing to nearest neighbor

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

  • Using Storyboards in Xcode 7 - Techotopia
    the mouse button a menu will appear Select the Present Modally menu option to establish the segue Figure 25 5 As more scenes are added to a storyboard it becomes increasingly difficult to see more than a few scenes at one time on the canvas To zoom out double click on the canvas To zoom back in again simply double click once again on the canvas Ctrl clicking on the storyboard canvas background will provide a menu containing a number of zoom level options Configuring Storyboard Transitions Xcode provides the option to change the visual appearance of the transition that takes place during a segue To change the transition select the corresponding segue line display the Attributes Inspector View Utilities Show Attributes Inspector and modify the Transition setting In Figure 25 6 the transition has been changed to Cross Dissolve Figure 25 6 If animation is not required during the transition turn off the Animates option To delete a segue from a storyboard simply select the segue line in the storyboard canvas and press the keyboard delete key Compile and run the application Note that touching the Go to Scene 2 button causes Scene 2 to appear Associating a View Controller with a Scene At this point in the example we have two scenes but only one view controller the one created by Xcode when we selected Single View Application Clearly in order to be able to add any functionality behind scene 2 it too will need a view controller The first step therefore is to add the class source file for a view controller to the project Ctrl click on the Storyboard target at the top of the project navigator panel and select New File from the resulting menu In the new file panel select Source listed under iOS in the left hand panel followed by Cocoa Touch Class in the main panel and click Next to proceed On the options screen verify that the Subclass of menu is set to UIViewController and that the Also create XIB file option is deselected since the view already exists in the storyboard there is no need for an XIB user interface file and name the class Scene2ViewController Select the Main storyboard file in the project navigator panel and select the View Controller button located in the panel above the Scene 2 view as shown in Figure 25 7 Figure 25 7 With the view controller for scene 2 selected within the storyboard canvas display the Identity Inspector View Utilities Identity Inspector and change the Class from UIViewController to Scene2ViewController Figure 25 8 Scene 2 now has a view controller and corresponding Swift source file where code may be written to implement any required functionality eBookFrenzy com Purchase the full edition of this iOS 9 App Development Essentials book in eBook 12 99 or Print 39 99 format iOS 9 App Development Essentials Print and eBook ePub PDF Kindle edition contains 105 chapters Select the label object in scene 2 and display

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



  •