Modern Auto Layout Building Adaptive Layouts for Ios

Modern Auto Layout Building Adaptive Layouts for Ios

Modern Auto Layout Building Adaptive Layouts For iOS Keith Harrison Web: useyourloaf.com Version: 5.0 (2020-10-28) Copyright © 2020 Keith Harrison Contents 1Introduction 1 Why Learn Auto Layout? ........................ 1 Modern Auto Layout ........................... 2 Before We Get Started .......................... 2 What We Will Cover ........................... 4 Get The Code ............................... 5 Changes In This (Fifth) Edition .................... 6 Changes In The Fourth Edition ..................... 6 2LayoutBeforeAutoLayout 8 Our First Layout ............................. 9 Autoresizing ................................ 12 Creating A Custom Subclass Of UIView ............... 20 Layout Without Storyboards ...................... 28 Key Points To remember ......................... 44 Test Your Knowledge ........................... 45 3 Getting Started With Auto Layout 49 What Is Auto Layout? .......................... 49 What Is A Constraint? .......................... 50 Who Owns A Constraint? ........................ 55 How Many Constraints Do I Need? .................. 58 Test Your Knowledge ........................... 65 4UsingInterfaceBuilder 70 The Many Ways To Create A Constraint ............... 70 Editing A Constraint ........................... 77 Creating Outlets For Constraints ................... 79 Viewing Layout Warnings And Errors ................. 81 Interface Builder Example ....................... 83 Interface Builder Tips And Tricks ................... 93 Test Your Knowledge ........................... 99 5CreatingConstraintsInCode 104 Activating and Deactivating Constraints ...............104 Disabling The Autoresizing Mask ...................106 i Creating Constraints With NSLayoutConstraint ..........107 Visual Format Language ........................110 Layout Anchors ..............................113 Which Should You Use? .........................118 Constraints In A Custom View .....................119 Key Points To Remember ........................122 Test Your Knowledge ...........................123 6SafeAreasAndLayoutMargins 127 Safe Area Layout Guide .........................128 Layout Margins ..............................144 Layout Guides ...............................158 Key Points To Remember ........................166 Test Your Knowledge ...........................167 7LayoutPrioritiesAndContentSize 171 Layout Priorities .............................171 Intrinsic Content Size ..........................181 Content Mode ...............................187 Content Hugging And Compression Resistance ...........190 Key Points To Remember ........................198 Test Your Knowledge ...........................199 8StackViews 204 Getting Started With Stack Views ...................205 ACloserLookAtStackViews .....................213 Stack Views And Layout Priorities ...................223 Dynamically Updating Stack Views ..................230 Adding Background Views .......................234 Stack View Oddities ...........................240 Key Points To Remember ........................245 Test Your Knowledge ...........................246 9UnderstandingTheLayoutEngine 251 The Layout Pass ..............................251 Should You Use updateConstraints? .................254 Animating Constraints ..........................255 Custom Layouts ..............................258 Alignment Rectangles ..........................264 Key Points To Remember ........................269 Test Your Knowledge ...........................270 10Debugging When It Goes Wrong 275 Unsatisfiable Constraints ........................275 Adding Identifiers To Views And Constraints ............282 Ambiguous Layouts ...........................283 Using The View Debugger .......................285 Private Debug Methods .........................287 ii Layout Loops ...............................292 Key Points To Remember ........................294 11Scroll Views And Auto Layout 296 Creating Constraints For A Scroll View ...............296 Scrolling A Stack View ..........................305 Frame And Content Layout Guides (iOS 11) .............311 Managing The Keyboard ........................319 Key Points To Remember ........................324 Test Your Knowledge ...........................324 12Dynamic Type 329 Using Dynamic Type ...........................329 Readable Content Guides ........................342 Text Views .................................347 Scaling Dynamic Type ..........................350 Custom Fonts With Dynamic Type ...................354 Key Points To Remember ........................364 Test Your Knowledge ...........................365 13Working With Table Views 370 Table View Row Height .........................370 Self-Sizing Table View Cells ......................371 Readable Table Views ..........................383 Self-Sizing Table View Headers ....................387 Key Points To Remember ........................399 Test Your Knowledge ...........................401 14Adapting For Size 405 Trait Collections .............................406 Size Classes ................................407 Supporting iPad Multitasking .....................410 Using Size Classes With Interface Builder ..............412 Using Traits In Code ...........................425 Using Traits With The Asset Catalog .................437 Variable Width Strings ..........................441 When Size Classes Are Not Enough ..................446 Key Points To Remember ........................452 Test Your Knowledge ...........................453 ATourOfInterfaceBuilder 460 Xcode Toolbar ...............................461 Inspectors .................................461 Library ...................................462 Document Outline ............................463 Device Configuration ...........................465 Configuring The Editor .........................465 Assistant Editor ..............................466 iii Previewing Your Layout .........................468 Auto Layout Tools .............................470 BLayoutEssentials 472 The View Hierarchy ...........................472 View Geometry ..............................477 CPointsvs.Pixels 483 One More Thing 486 iv Chapter 1 Introduction You may have heard Auto Layout described as a constraint-based layout engine. What does that mean? Do you need to know math and write equations? Why is that any better than manually calculating the size and position of each view in your layout? Have you been resisting using Auto Layout? Maybe you tried it and gave up in frustration? Or maybe you’re new to iOS development and wondering how to get started. Well, this book is for you. Why Learn Auto Layout? Apple first introduced us to Auto Layout in OS X 10.7 Lion. It took a while longer to come to iOS developers as part of iOS 6 unveiled at WWDC 2012. Auto Layout promises to make your layouts simpler to write, easier to understand, and less effort to maintain. Using Auto Layout can feel a little abstract at first. Instead of manually setting the frame of each view you describe the relationships between your views with constraints and Auto Layout sets the frames for you. The advantage comes when your layout needs to respond and adapt to changes. Dynamic sizing needs a dynamic layout. A modern iOS App needs to adapt to a broad set of user interface situations: • Layouts need to scale from the smallest device like the iPhone SE up to the largest 12.9" iPad Pro and work in slide over and split screen modes. • Text size can change significantly with localization and even more dramatically with dynamic text. Paragraphs of text that fit comfort- 1 Modern Auto Layout 1. Introduction ably at small text sizes can grow to where one word fills the screen at the largest of the accessibility sizes. • You need to be able to quickly adapt when Apple introduces new devices like the iPhone X with a top sensor housing and home screen indicator. You don’t have to use Auto Layout, but many of the above challenges become manageable when you describe the relationships between your views with constraints. For example, layouts built with Auto Layout for the iPhone X in 2017 only needed rebuilding with the iOS 12 SDK to support the new screen sizes of the iPhone XR and iPhone XS Max introduced in 2018. Modern Auto Layout What do I mean by “Modern Auto Layout”? A lot has changed over the years since Apple introduced Auto Layout in iOS 6. For me, Modern Auto Layout began with iOS 9: • In iOS 9, Apple added layout anchors and layout guides. They also added stack views and using Auto Layout got a whole lot less painful. • In iOS 10, adopting Dynamic type became less work with automatic font adjustments to content size changes. • In iOS 11, safe area layout guides and safe area relative margins replaced top and bottom layout guides. You can change the margins of the root view. Scroll views got layout guides, and stack views got custom spacing. • In iOS 12 through iOS 14 the Auto Layout API has remained mostly stable. Apple improved the performance of Auto Layout in iOS 12, tweaked the handling of trait collection changes in iOS 13, and changed the stack view implementation in iOS 14 making it respond to background colors. Before We Get Started I assume you have a basic knowledge of iOS app development. You should be comfortable using Xcode to create an app and run it on the simulator or device. This book doesn’t teach you Swift or Objective-C programming or much about App architecture. I’ve used Swift for the code examples but don’t worry if you’re not an expert Swift programmer. 2 Modern

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    47 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us