iOS Programming Cookbook
eBook - ePub

iOS Programming Cookbook

  1. 520 pages
  2. English
  3. ePUB (mobile friendly)
  4. Available on iOS & Android
eBook - ePub

iOS Programming Cookbook

Book details
Book preview
Table of contents
Citations

About This Book

Over 50 exciting and powerful recipes to help you unearth the promise of iOS programmingAbout This Book• Create high performance iOS apps with a focus on application development APIs and techniques• Enrich your UI skills with UIStoryboard, Autolayout, Size classes, and Container view• Produce enhanced results with iOS 10 as a result of learning and implementing pro-level practices, techniques, and solutionsWho This Book Is ForIf you are an iOS developer on a quest to develop your perfect iOS app, then this book is for you. It would also prove to be a valuable resource for those who want to get up and running with iOS development through a clear, practical approach. In order to unleash the full potential of this book, basic Swift programming knowledge is necessary.What You Will Learn• Build your own custom UIViews through code or the interface builder• Implement a dynamic and interactive interface in an iOS app• Work on various graphics related elements and the process of using them together to make meaningful shapes.• Use the side over and split view to interact with multiple apps concurrently• Encrypt JSON calls to make the app more secure• Work on web markup feature to enhance search optimizationIn DetailDo you want to understand all the facets of iOS programming and build complex iOS apps? Then you have come to the right place. This problem-solution guide will help you to eliminate expensive learning curves and focus on specific issues to make you proficient at tasks and the speed-up time involved.Beginning with some advanced UI components such as Stack Views and UICollectionView, you will gradually move on to building an interface efficiently.You will work through adding gesture recognizer and touch elements on table cells for custom actions. You will work with the Photos framework to access and manipulate photos. You will then prepare your app for multitasking and write responsive and highly efficient apps. Next, you will integrate maps and core location services while making your app more secure through various encryption methods. Finally, you will dive deep into the advanced techniques of implementing notifications while working with memory management and optimizing the performance of your apps. By the end of the book, you will master most of the latest iOS 10 frameworks.Style and approachThis is the best practical resource on iOS 10 development. This book, with its no nonsense approach and a clear practical focus, will be your best friend on your quest to develop your perfect iOS app. The best thing about this book is that in addition to recipes on iOS programming techniques and app development essentials, it will take you on a complete guided tour of all the new app development APIs that are shipped with iOS 10.

Frequently asked questions

Simply head over to the account section in settings and click on “Cancel Subscription” - it’s as simple as that. After you cancel, your membership will stay active for the remainder of the time you’ve paid for. Learn more here.
At the moment all of our mobile-responsive ePub books are available to download via the app. Most of our PDFs are also available to download and we're working on making the final remaining ones downloadable now. Learn more here.
Both plans give you full access to the library and all of Perlego’s features. The only differences are the price and subscription period: With the annual plan you’ll save around 30% compared to 12 months on the monthly plan.
We are an online textbook subscription service, where you can get access to an entire online library for less than the price of a single book per month. With over 1 million books across 1000+ topics, we’ve got you covered! Learn more here.
Look out for the read-aloud symbol on your next book to see if you can listen to it. The read-aloud tool reads text aloud for you, highlighting the text as it is being read. You can pause it, speed it up and slow it down. Learn more here.
Yes, you can access iOS Programming Cookbook by Hossam Ghareeb in PDF and/or ePUB format, as well as other popular books in Computer Science & Operating Systems. We have over one million books available in our catalogue for you to explore.

Information

Year
2017
ISBN
9781786467706
Edition
1

The Essentials

In this chapter, we will cover the following topics:
  • Using UIView via code or interface builder to build your own custom views
  • Working with navigation controller and navigation bar
  • Working with stack views
  • Working with UICollectionView
  • Working with gestures like swipe, pan, rotation, and tap
  • Using 3D touch

Introduction

Game on, your swift weapons are ready for iOS development. This chapter will be considered your first station in our journey through this book. We will cover some of the most commonly used UI components for building your app screens in this chapter. We will start with the godfather of all UI components--the UIView. Then, we will introduce navigation controller, stack views, collection views, and the gesture recognizers. These components are important because you will use them frequently in iOS development and should be known by any iOS developer. This is not applicable to the last recipe (3D Touch), since it's a new feature in iOS devices; but, it's still important, as all new upcoming devices will have this feature, so you should be ready and be aware of how to utilize it in your app.

Using UIView via code or interface builder to build your own custom views

UIViews are the base building blocks of any iOS app. Think of LEGO; kids build their own buildings and blocks using tiny base blocks. The same logic is used in iOS; all UI screens you see are just a building of UIViews. All native/custom UI components extend from UIView; in other words, UIView is the base class of all UI components. To master iOS development and building the layout of any app, you have to be familiar with UIView.

Getting ready

We will see in this recipe how to create/use UIViews programmatically (hardcoded) or via interface builder. The fast and recommended way is to create your UIViews via interface builder (XIB files or Storyboards); but of course, in some cases, you will need to build custom UIViews, and in that case you will build your own custom UIViews programmatically.

How to do it...

  1. Go to Xcode and create a new iOS project with template Single View Application. Set the name of the project to UIViews.
  2. Now, select the storyboard file and open the single page view controller:
  1. In the Attribute Inspector in the right-hand side bar, set the size of the View Controller to iPhone 4.7-inch size.
  2. Open the View Controller view, and from Object Library in the right-hand side bar, drag two UIViews. In the first UIView and from the Size Inspector tab in the right side bar, set the frame of the first one to (x = 0, y = 0, width = 375, height = 300), and from the Attribute Inspector tab, set red as the background color. In the second UIView, set the frame to (x = 0, y = 300, width = 375, height = 300).
  1. Rename the two views by selecting each one and hit the Enter key. The title will be converted to a text field where you can rename your views. Change the first view to RedView and the second one to BlueView. You should see something like this:
  1. Take an outlet for your views. Click on Assistant Editor at the top bar to open the ViewController.swift file, and drag the view to the source code file to add outlets by selecting each view and holding the Ctrl key:
  1. Now, open the ViewController.swift file in the viewDidLoad function and add the following code:
 let yellowView = UIView(frame: CGRectMake(0, 0, 200, 100)) 
yellowView.backgroundColor = UIColor.yellowColor()
self.redView.addSubview(yellowView)

let brownView = UIView(frame: CGRectMake(100, 50, 200, 100))
brownView.backgroundColor = UIColor.brownColor()
self.redView.insertSubview(brownView, belowSubview: yellowView)
  1. Now, build and run the project; you should see something like this:

How it works...

First, we created the project with the Single View template; there are multiple templates that you can use or start with for your empty project. Then, we set the layout size of the view controller to a 4-inch size iPhone, as the project is intended to be for iPhone only, not iPad. This doesn't mean that your app will work on a 4-inch (iPhone 6 and iPhone 6s) iPhone only; it indicates what the layout will look like. We will see in later chapters how to design your app's UI, regardless of the screen size via size classes and auto layout.
Then, we saw how simple it is to add UIViews via interface builder. We added the red and blue views without wiring any line of code, and you can see how to change the view's properties in Attribute Inspector.
We add outlets to your views in the ViewController.swift file, which act as a reference to your components created in interface builder. So, you can access them any time in your code and do any specific action on them, such as hiding, resizing, translation, or adding subviews.
We then moved to source code to add two views, but programmatically. You first need to initialize the UIView and we used one of its initializers, which takes a frame to initialize with. The frame specifies the size and location (on superview) of the view. Then, we change the background color using one of its properties: .backgroundColor. I recommend opening the documentation of UIView, where you will see a list of properties and functions to be used.
Creating UIViews is nothing without presenting them onscreen, and to present it, you have to add a subview to any other view. The most common method used is addSubview(), which adds the view as a subview to the superview and on top of the view hierarchy. In some cases, adding on the top is not what you want, and you need some flexibility to choose where to add your view in the view hierarchy; that's why, UIView provides you with another three methods to insert a subview:
  • Function insertSubview(brownView, belowSubview: yellowView): This adds a subview below any other subview that you have in your view hierarchy. This method requires having a reference to the view you want to add below a subview.
  • Function insertSubview(view, atIndex: 2): This is a very flexible function to add any subview to your view hierarchy at any index. The index is 0 indexed, and 0 means the first subview.
  • Function insertSubview(view, aboveSubview: superview): This adds a subview above any other subview you have in your view hierarchy. This method requires having a reference to the view you want ...

Table of contents

  1. Title Page
  2. Copyright
  3. Credits
  4. About the Author
  5. About the Reviewer
  6. www.PacktPub.com
  7. Customer Feedback
  8. Preface
  9. Swift Programming Language
  10. The Essentials
  11. Integrating with Messages App
  12. Working with Interface Builder
  13. Working with UITableView
  14. Animations and Graphics
  15. Multimedia
  16. Concurrency
  17. Location Services
  18. Security and Encryption
  19. Networking
  20. Persisting Data with Core Data
  21. Notifications
  22. App Search
  23. Optimizing Performance