• Skip to main content
  • Skip to primary sidebar

Ravi Shankar

Tweaking Apps

  • About
  • Portfolio
  • Privacy Policy

Archives for July 2015

UICollectionView Demo in Swift

July 31, 2015 By Ravi Shankar

UICollectionView can be used for displaying set of data in rows and columns The main difference between UICollectionView and UITableView is that CollectionView can be display more than one column. The following topics are covered in this article

  • Simple collection view demo
  • Displaying data in Collection View
  • Implementiing Custom Cell 
  • Adding Section Headers
  • Highlighting Cell
  • Insert Cell
  • Delete Cells

Project Setup

Create a new project by selecting Single View Application template.

Provide the necessary details in the Project options screen and select the language as Swift.

Adding CollectionView

Let us first try out simple collection view to get a better understanding of how various components works. Then let us move on to a demo that displays various fruits grouped in different section. And you will be able insert and delete cells from the Collection View.

Navigate to Main.storyboard, disable Auto Layout and size classes using File Inspector option.

Then drag and drop CollectionView from object library to ViewController. The ViewController with CollectionView should look as shown below.

The square box inside collection view is UICollectionViewCell. Using Attributes Inspector, change the background colour of CollectionView to white. Then select UICollecitonViewCell and enter value for identifier as “CellIdentifier”

Using Assistant Editor, add an IBOutlet to CollectionView in ViewController.swlft file.

[code language=”swift”]@IBOutlet weak var collectionView: UICollectionView![/code]

Implement UICollectionViewDataSource methods

When the CollectionView loads, we need to specify the data for the cells. This can be done by implementing
UICollectionViewDataSource related methods. UICollectionViewDataSource protocols defines the following mandatory and optional methods.

[code language=”swift”]protocol UICollectionViewDataSource : NSObjectProtocol {

func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int

// The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath:
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell

optional func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int

// The view that is returned must be retrieved from a call to -dequeueReusableSupplementaryViewOfKind:withReuseIdentifier:forIndexPath:
optional func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView
}[/code]

First make sure to set the delegate property of collectionView to self in viewDidLoad function. Then add instance level property for storing Cell Identifier.

[code language=”swift”]let identifier = "CellIdentifier"

override func viewDidLoad() {
super.viewDidLoad()

collectionView.dataSource = self
}[/code]

Now add the implementation for mandatory methods numberOfItemsInSection and cellForItemAtIndexOath in ViewController.swlft. We can do this by adding an extension to ViewController class. Add this extension after the closing parenthesis of View Controller class.

[code language=”swift”]// MARK:- UICollectionViewDataSource Delegate
extension ViewController: UICollectionViewDataSource {

func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 12
}

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier(identifier, forIndexPath: indexPath) as! UICollectionViewCell
cell.backgroundColor = UIColor.redColor()

return cell
}
}
[/code]

We have temporarily hard coded the number of items to be shown as 12. Using dequeReusableCellWithReuseIdentifier funciton, create cells based on index path. Then change the background color of cell to red. Now when you build and run the project, you should see collection view showing some cells as shown below.

If you want to have 3 rows and 4 columns then use the Collection View’s Size Inspector to make the appropriate changes to Cell Size and Minimum Spacing attributes.

If you Build and Run the project you should notice the changes.

Collection View DataSource

Now for the actual demo project, let us create a seperate class which will act as DataSource. Before creating a datasource, let us create a model class with file name as Fruit.swift. The implementation of the Fruit class should look as shown below

[code language=”swift”]class Fruit {
var name:String?
var group:String?

init(name: String, group: String) {
self.name = name
self.group = group
}
}
[/code]

 

Fruit struct is just a place holder for storing fruit related information. Now create another class for DataSource and name it as DataSource.swift. This class will provide the data related methods to the CollectionView. Durining the initialisation of the class, the data is read from plist and populated to fruits and groups array. Then using the respective helper methods the details will be retrieved by CollectionView.

[code language=”swift”]import Foundation

class DataSource {

init() {
populateData()
}

var fruits:[Fruit] = []
var groups:[String] = []

func numbeOfRowsInEachGroup(index: Int) -> Int {
return fruitsInGroup(index).count
}

func numberOfGroups() -> Int {
return groups.count
}

func gettGroupLabelAtIndex(index: Int) -> String {
return groups[index]
}

// MARK:- Populate Data from plist

func populateData() {
if let path = NSBundle.mainBundle().pathForResource("fruits", ofType: "plist") {
if let dictArray = NSArray(contentsOfFile: path) {
for item in dictArray {
if let dict = item as? NSDictionary {
let name = dict["name"] as! String
let group = dict["group"] as! String

let fruit = Fruit(name: name, group: group)
if !contains(groups, group){
groups.append(group)
}
fruits.append(fruit)
}
}
}
}
}

// MARK:- FruitsForEachGroup

func fruitsInGroup(index: Int) -> [Fruit] {
let item = groups[index]
let filteredFruits = fruits.filter { (fruit: Fruit) -> Bool in
return fruit.group == item
}
return filteredFruits
}
}[/code]

Then add the required images to Images.xcassets, you can download the images for this project from GitHub.

Also add/create a new plist file which contains the information about the fruits and the group they belong to (download it from here).

Add Custom CollectionViewCell

For displaying image and caption in Collection View Cell, create a Custom Cell subclass of UICollectionViewCell. Provide name for the new file as Fruit Cell.

Navigate to Main.storyboard, select CollectionViewCell and using identity inspector set the class as FruitCell.

Drag and drop UIImageView and Label on to CollectionViewCell and add corresponding IBOutlets to FruitCell class.

[code language=”swift”]
import UIKit

class FruitCell: UICollectionViewCell {

@IBOutlet weak var caption: UILabel!
@IBOutlet weak var imageView: UIImageView!
}
[/code]

Display Data

We need to make changes to the ViewController extension for displaying the data form the DataSource with CustomCell. First Create an instance variable for DataSource in ViewController class

[code language=”swift”]let dataSource = DataSource()
[/code]

Then make the following changes to UICollectionViewDataSource extension to reflect the DataSource and FruitCell classes.

[code language=”swift”]
extension ViewController : UICollectionViewDataSource {

func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
return dataSource.groups.count
}

func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return dataSource.numbeOfRowsInEachGroup(section)
}

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier(identifier,forIndexPath:indexPath) as! FruitCell

let fruits: [Fruit] = dataSource.fruitsInGroup(indexPath.section)
let fruit = fruits[indexPath.row]

let name = fruit.name!

cell.imageView.image = UIImage(named: name.lowercaseString)
cell.caption.text = name.capitalizedString

return cell
}
}[/code]

Now if you build and run the project, you should see the colleciton view displaying fruits along with the caption.

The rows and columns are not properly aligned, we can fix this calculate the size of the cell based on height and width of collection view. Let us make the Collection View to display 2 cells per row. Navigate to Main,storyboard, update the Cell Size property to 182 as shown below.

Make sure to adjust the UIImageView and Label to fit the changed Collection View cell size. Now if you compile and run the project, the simulator should look as shown below.

Add Section Header

Headers for each can be added by implementing the viewForSupplementaryElementOfKind method defined as part of UICollectionViewDataSource protocol. We already have function in DataSource class that returns caption for each section. Let us add new Custom class for CollectionView section header and map this class to the header view in Interface builder.

Create FruitsHeaderView with subclass as UICollectionResuableView. Then navigate to Main.storyboard, select Collection View -> Attributes Inspector and enable Section Header under Accessories.

Now select the Section Header in the Collection View and set the class as FruitsHeaderView using Identity Inspector. In the Attributes Inspector enter the Identifier as HeaderView

Add UILabel to the header view to display the seciton title and corresponding IBOutlet to FruitsHeaderView class. You can also provide some background colour for the HeaderView,

[code language=”swift”]
import UIKit

class FruitsHeaderView: UICollectionReusableView {
@IBOutlet weak var sectionLabel: UILabel!
}
[/code]

Now add the following viewForSupplementaryElementOfKind implementation to the UICollectionViewDataSource extension in ViewController class.

[code language=”swift”] func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView {

let headerView: FruitsHeaderView = collectionView.dequeueReusableSupplementaryViewOfKind(kind, withReuseIdentifier: headerViewIdentifier, forIndexPath: indexPath) as! FruitsHeaderView

headerView.sectionLabel.text = dataSource.gettGroupLabelAtIndex(indexPath.section)
return headerView
}
[/code]

 

Make sure to add an instance variable let headerViewIdentifier = “HeaderView” in ViewController.class. In viewForSupplementaryElementOfKind function, we are creating an instance of FruitsHeaderView class using dequeueReusableSupplementaryViewOfKind function. Then set the section label by retrieving the caption from DataSource class. Build and run the app on iPhone simulator should show the following

Add Detail View

Now to add a detail View, let us first embed the ViewController in Navigation Controller. Then add another ViewController for using it as Detail View. Create a segue by Control + drag from CollectionView Cell to the new View Controller and select the Segue as Push.

Add UIImageView to the DetailViewController and centre align it to the View. Then add a new class (sub class of UIViewController) and name the file as DetailViewController

Map this class to the Second View Controller in the Interface builder. Then add the IBOutlet for the UIImageView in DetailViewController class.

@IBOutlet weak var imageView: UIImageView!

From the main View Controller, the selected Fruit needs to be passed to the DetailViewController class. Add a new property which is of type Fruit

[code language=”swift”]var fruit: Fruit?[/code]

In viewDidLoad method, add code to populate the title and image.

[code language=”swift”]if let fruit = fruit {
navigationItem.title = fruit.name?.capitalizedString
imageView.image = UIImage(named: fruit.name!.lowercaseString)
}[/code]

Navigate to ViewController.class and implement the prepareForSegue and getIndexPathForSelectedCell function.

[code language=”swift”]// MARK:- prepareForSegue
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// retrieve selected cell & fruit
if let indexPath = getIndexPathForSelectedCell() {

let fruit = dataSource.fruitsInGroup(indexPath.section)[indexPath.row]

let detailViewController = segue.destinationViewController as! DetailViewController
detailViewController.fruit = fruit
}
}

func getIndexPathForSelectedCell() -> NSIndexPath? {

var indexPath:NSIndexPath?

if collectionView.indexPathsForSelectedItems().count > 0 {
indexPath = collectionView.indexPathsForSelectedItems()[0] as? NSIndexPath
}
return indexPath
}
[/code]

In the above function using the selected Item indexPath the corresponding fruit is retrieved from DataSource class. Then this information is passed to the DetailViewController.

Highlight Selection

When the user taps any cell, it would nice to see the cell getting highlighted. This can be done by implementing following function as extension.

[code language=”swift”]// MARK:- UICollectionViewDelegate Methods

extension ViewController : UICollectionViewDelegate {
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
highlightCell(indexPath, flag: true)
}
}

func collectionView(collectionView: UICollectionView, didDeselectItemAtIndexPath indexPath: NSIndexPath) {
highlightCell(indexPath, flag: false)
}
}[/code]

Also make sure to add the collectionView.delegate = self to viewDidLoad function. Then add the following highlight function inside ViewController class.

[code language=”swift”]// MARK:- Highlight
func highlightCell(indexPath : NSIndexPath, flag: Bool) {

let cell = collectionView.cellForItemAtIndexPath(indexPath)

if flag {
cell?.contentView.backgroundColor = UIColor.magentaColor()
} else {
cell?.contentView.backgroundColor = nil
}
}
[/code]

 

Since we want to dehighlight the cell when the user returns from the DetailViewController. implement the viewDidAppear function with de-hightlight functionality.

[code language=”swift”]override func viewDidAppear(animated: Bool) {
super.viewDidAppear(true)

if let indexPath = getIndexPathForSelectedCell() {
highlightCell(indexPath, flag: false)
}
}
[/code]

Insert Cell

CollectionView provides insertItemsAtIndexPath method for adding new cell to CollectionView. Navigate to Main.storyboard, add new BarButtonItem to ViewController and set the Identifier as Add.

Add an IBAction to ViewController class and map it to the Add button. In the DataSource class add the following function which inserts new item to fruit model and returns the index.

[code language=”swift”]
// MARK:- Add Dummy Data
func addAndGetIndexForNewItem() -> Int {

let fruit = Fruit(name: "SugarApple", group: "Morning")

let count = fruitsInGroup(0).count
let index = count > 0 ? count – 1 : count
fruits.insert(fruit, atIndex: index)

return index
}
[/code]

Then modify the addNewItem IBAction method with the following piece of code.

[code language=”swift”] // MARK:- Add Cell
@IBAction func addNewItem(sender: AnyObject) {
let index = dataSource.addAndGetIndexForNewItem()
let indexPath = NSIndexPath(forItem: index, inSection: 0)
collectionView.insertItemsAtIndexPaths([indexPath])
}
[/code]

Delete Cell

Add Edit button to the navigation bar to allow users to perform delete operation. This can be done by adding the following line in viewDidLoad method.

[code language=”plain”]navigationItem.leftBarButtonItem = editButtonItem()[/code]

Then add a toolbar with button to delete the selected cell. Navigate to Main.storyboard, drag and drop toolbar on to View Controller. Add a BarButtonItem to the toolbar and select the identifier as Trash.

This toolbar should be displayed when the user taps on Edit button. Create an IBOutlet for the toolbar and add the following line to the viewDidLoad method.

[code language=”swift”]toolBar.hidden = true[/code]

Implement the setEditing function to enable or disable editing operation. In the below function, when editing is enabled, users will be allowed to select and delete multiple cells. The toolbar will be displayed or hidden based on editing flag.

[code language=”swift”]
// MARK:- Editing
override func setEditing(editing: Bool, animated: Bool) {
super.setEditing(editing, animated: animated)
collectionView?.allowsMultipleSelection = editing
toolBar.hidden = !editing
}
[/code]

Now hook up Trash button in the Interface builder to IBAction for performing the delete operation. Add the following code snippet to deleteCells IBAction method. This first retrieves the indexpath for all the selected items. Then iterates through the indexpaths and to get the list of fruits to be deleted from model.

[code language=”swift”]
@IBAction func deleteCells(sender: AnyObject) {

var deletedFruits:[Fruit] = []

let indexpaths = collectionView?.indexPathsForSelectedItems()

if let indexpaths = indexpaths {

for item in indexpaths {
let cell = collectionView!.cellForItemAtIndexPath(item as! NSIndexPath)

collectionView?.deselectItemAtIndexPath(item as? NSIndexPath, animated: true)
// fruits for section
let sectionfruits = dataSource.fruitsInGroup(item.section)
deletedFruits.append(sectionfruits[item.row])
}

dataSource.deleteItems(deletedFruits)

collectionView?.deleteItemsAtIndexPaths(indexpaths)
}
}
[/code]

 

And in the DataSource class, add a function to delete fruits from the model. Also add an extension to array to get the index of the object based on the selected item (This will not be needed in Swift 2.0).

[code language=”swift”]
// MARK:- Delete Items
func deleteItems(items: [Fruit]) {

for item in items {
// remove item
let index = fruits.indexOfObject(item)
if index != -1 {
fruits.removeAtIndex(index)
}
}
}
[/code]

We need to cancel the segue operation when the edit operation is enabled, you can do this by implementing the following method in ViewController class.

[code language=”swift”]
// MARK:- Should Perform Segue
override func shouldPerformSegueWithIdentifier(identifier: String?, sender: AnyObject?) -> Bool {
return !editing
}[/code]

Download the source code from here

Filed Under: Swift, UICollectionView, Xcode Tagged With: Delete Cell, Insert Cell

Adaptive Layout in iOS

July 27, 2015 By Ravi Shankar

Adaptive Layout was introduced in iOS 8 to address the problem of designing user interface for different devices and screen sizes. In the earlier version of iOS, we had to maintain different storyboards for iPad and iPhone and with Adaptive Layout we just need one storyboard for all the devices. Some of the main components of Adpative Layout are Auto Layout, Size Classes and Trait Collections. Let us try to understand Adpative Layout by designing the following User Interface for various devices and screen sizes.

Create an universal project choosing Single View Application template. You can download the project from GitHub to get the images used in this demo and add them to Images.xcassets.

iPhone (Portrait)

Make sure you have selected the size classes as wAny hAny in the Storyboard.

Add a UIImageView, button and 2 labels then provide necessary details as shown below.

First let us design the screen for iPhones in Portrait mode by keeping Preview (Assistant Editor) adjacent to Interface builder.

Select the iPhone 6 caption label, align it centre horizontally to the container by using the Align option.

Set the distance from top layout to the label as 5 using the Pin option.

Now select the imageview and align it centre horizontally and Vertically to the container by using the Align option.

The specify the width and height constraint for the imageview using the Pin option. This would ensure that the size of imageview is uniform in all the devices.

Now select the Buy Now button and add contraints to the trailing and bottom of the button. Control + drag from button to View and select Trailing space to container margin.

Repeat Control + drag from button to the bottom of the View and select Bottom Space to Bottom Layout Guide.

Then using the Pin option add width and height constraints for the button.

Finally the select the label which is used for displaying the price and Control + drag from label to the container.

Edit the Trailing Space constraint for Price label with Buy Now button and set it to 5.

Previewing the screen changes for iPhone 3.5 and iPhone 4 should look as shown below

iPad (Portrait & Landscape)

iPad has lots of screen space compared to an iPhone. Let us to see how to maximize the screen usage of an iPad by adding an extra image.

Set the size classes to wRegular hRegular to design user interfaces for iPad and iPhone. The imageview is currently aligned to centre both horizontally and vertically. We need to remove the horizontal constraint (Center X) so that the imageview can be moved towards the left handside. This should create an extra space to add another imageview to the View Controller.

Disable the Center X constraint for iPhone by navigating to Size Inspector and press the delete key.

Then pin the imageview to left side of the view without setting any margin (Pin option).

Use the Update Frame option under Resolve Auto Layout Issues to move the views based on the applied constraints. Add another ImageView to View Controller then select an iPhone 6 black image and set the view mode to Aspect Fit.

Control + drag from the imageview on the right hand side to imageview on the left hand side. Select Center Y from the list of available constraints.

Changing Font Size for Size Classes

Adaptive Layout allows you to specify different font size based on the size class. In the Show Attributes Inspector, click the + sign next Font option and choose wRegularWidth hRegularHeight option. Now you can customize the font properties specific to iPad

Now you should see the extra image just for iPads in portrait and Landscape mode. Still there are lots of empty space but you can fill them as part of your code challenege :-). May be you can other details such as add description, rating button etc..

When you navigate back to wAny hAny size classes, the document outline should show the new image as greyed out. As the second imageview is available only in regular size classes. Also the attributes inspector for the second imageview should provide the details on installed size classes.

The above screenshot shows that the control installed only in wR wH size classes and not wAny hAny size classes.

iPhone (Landscape)

When previewing the design in both portrait and landscape, you must have noticed that some controls are hidden in landscape mode. This can be fixed by making necessary changes to compact size classes.

Set the size classes to wAny hCompact in the storyboard. Select the imageview, navigate to Size Inspector and disable the Center X constraint.

Adjust the size of imageview if it is too large in landscape mode. To reflect frame changes, select Update Constraint under Resolve Auto Layout Issues option. Another way is by editing the width and height constraint for the imageview.

Since we want to move the price lable above the Buy Now button, remove all the constraints associated with the label.

Move the label just above the Buy Now button and centre align to the imageview.

Control + drag from Price label to Buy Now button and select Center X constraint.

Repeat the above step but this time from Price label to ImageView and select Center Y Constraint.

The final preview of screen in iPhone 3.5 portrait and landscape mode should look as shown below.

Download the source code from here

Summary

We have covered some basics of Adaptive Layout by designing user interface with size classes. changing font attibutes, installing controls,disable and editing constraints based on different size classes.

Filed Under: Adaptive Layout, Auto Layout, Size Classes

Hide worksheet formula in Excel 2013 & Excel 2010

July 26, 2015 By Ravi Shankar Leave a Comment

Excel 2013 and Excel 2010 allow users to hide formula used in a Worksheet. You can use this feature when you want to prevent any one from accidentally changing the entered formula.

Let us say you want to hide the formula (=SUM(F5:F8)) which has been used in the bellow example.

image

Select the cell where the formula has been entered and navigate to Format menu and click the Format Cell menu option (You can also right click and select Format Cell in the Context menu).

In the Format Cell window, navigate to Protection tab and tick the checkbox with Label as Hidden. Click the Ok button to save the changes.

image

Then right click on the Sheet and select Protect Sheet from the list of available menu option.

image

In the Protect Sheet window, tick the check box with label as Protect worksheet and contents of locked cells.

image

If you want to just hide formula then it is not needed to password protect the sheet. Click Ok button to save the changes.

Now when you navigate to the Formula Cell, the formula bar will not displayed any information.

image

Bu this you can prevent excel users from seeing formula used in the worksheet.

Filed Under: Excel, Excel 2010, Office 2010 Tagged With: Excel 2010, Format Cell, Formula, Hidden, hide, Protect, Worksheet

How to unhide comments in Excel

July 25, 2015 By Ravi Shankar 1 Comment

Excel 2013 and Excel 2010 generally shows comments in worksheet with the Indicator as shown below.

image

And when you hover over the indicator it will display the added comments.

image

But in case if you are not able see the comments added to your worksheet then try the unhide option available as part of the

  1. Click the File menu and then the Options link
  2. In the Excel Option window, navigate to Advanced Options
  3. Scroll down to display section and For cells with comments radio option select your preferred choice i.e either Indicators only, and comments on hover or Comments and indicators.
    4. Click Ok to save and confirm the changes.

image

Filed Under: Excel, Excel 2010, Office 2010 Tagged With: comments, Excel 2010, indicator, unhide comments

Auto Layout in iOS

July 25, 2015 By Ravi Shankar 5 Comments

Using Auto Layout feature developers can align UI controls for different devices, screen sizes and orientation. This reduces the code to be maintained and makes life easy for the developers. Auto Layout solves UI design issues by creating relationship between the UI elements. This follows constraint based layout system where you can define contraints for each UI elements. Let us see some basics of Auto Layout by looking at four different scenarios

The objective of this article is to provide details on the following

  • Different ways to add constraints to UI elements.
  • How to centre align a control to both vertically and horizantally to container.
  • Arrange three controls in single column with centre aligned horizontally to the container.
  • Design simple login form by embeding the controls inside view
  • Design a form where the width of controls gets adjusted based on device orientation.
  • Clear and Edit constraints.
  • Pinning constraint and Updating frames

Center Align image Vertical and Horizontal to the container

For this demo, let us start with a single view application project. Drag and drop an UIImageView from object libary on to View Controller. Copy an image to your project and set that image to UIImageView using Attributes Inspector (Download the image from GitHub project).

You might be familiar with Size Inspector under Xcode’s Utilies Pane. This helps us to enter the height, width, x and y position for any UI elements.

Another alternate way to enter the size and postion for elements by adding constraints using the Auto Layout option which is available at the bottom of the Interface builder.

Select the Click the Pin option, enter value for Width and Height as 300 then Add these 2 Constraints.

After applying the constraint, you should notice orange dashed line indicating that your control is out of position. Let us update the frame afer applying all the required constraints.

Click Align option (first option) and select Horizontal and Vertical Center in Container then Add these 2 Constraints.

If the ImageView on the ViewController is in misplaced position, the document outline will show an yellow color indication mark at top right hand corner. You can select that icon to find more details about the issue.

Document Outline

Document Outline in the Storyboard lists down the installed controls for a scene. Also you can the list of constraints added for these controls. If you do not need any constraint then you select it and delete them in Document Outline.

The missplaced views can be fixed by selecting Update Frames option under Resolve Auto Layout Issues (Auto Layout option with a triangle).

Preview User Interface changes

Now you can preview user interface changes on various device by using the Preview option. Click the icon for showing Assistant Editor window. Navigate to Preview and select Main.storyboard option as shown in the below screenshot.

You can use + sign to preview the screen in a specific device and also use rotate screen option to preview the changes in different screen orientation.

Arrange three Buttons in single column

In the next demo, we wil try to arrange three buttons aligned horizontally center to View and in a single column. Add a View Controller to the storyboard then drag drop three buttons on the View Controller. Though we have used the guidelines to align these buttons they don’t look the same when you preview them.

Now let us see how to fix these UI issues by adding constraints.

First select the Button with caption as Auto Layout 2 and make it Horizontal and Vertical Center in Container then Add these two constraints. This would make sure the Auto Layout 2 button is aligned center to the View both vertically and horizontally.

Select the button with caption as Auto Layout 1. Use the Aign option and add Horizontal Center in Container constraint.

Now you need to specify the Centre X and Verical Spacing constraint for this button. This can be done by control dragging from Auto Layout 1 button to Auto Layout 2 and you will be presented with the following option.

Select Vertical Spacing to position the control at constant distance from Auto Layout 2. Repeat the same control drag from Auto Layout 1 and Auto Layout 2 and this time choose Centre X.

Now repeat the above steps for Auto Layout 3 with Auto Layout 2 i.e add Centre X and Vertical Spacing constraints.

 

Scenario 3:- Adjust control width based on screen orientation

This scenario explains how to add constraints so that controls width increases or decreases based on the screen size. For example in the below screenshots, the width for textfields provided for entering name and Age gets adjusted based on screen orientation

Add a new View Controller on to Storyboard then add 2 labels and 2 textfields and a button. Provide the caption for both labels as Name, Age and Submit (button) respectively. We need to add constraints to labels so that their width and position are fixed at the specified location. And for both the textfields, constraints have to be added to trailing edge so that width increases or decreases based on the orientation

Add three constraints to the label with caption as Name using the pin option as shown in the below screenshot.

Repeat the same for label with caption as Age

Select the textfield adjacent to Name label, control drag from textfield to the container then select Trailing Space to Container Margin.

Need to align the textfield with the label, control drag from text field to name label and select Centre Y from the list of constraints.

Keeping the textField selected, navigate to Size Inspector (Utilities Pane) and cick Edit on Trailing Space constraint. Set the value for the constant as 10 as we want to maintain the space between textfield and container to 10 in both landscape and portrait mode.

Repeat the above steps done for Age textfields as well.

Finally we need to add two constraints for the Submit button to make sure it is aligned on the right hand side at certain distance from the Top Layout. Control drag from button to top of the container and select Top Space to Top Layout Guide.

Again repeat the above step by this time to side of the container and select Trailing Space to Container Margin.

Now prevewing the screen in Portait and Landscape mode should look as shown below.

Scenario 4:- Apply constraints to Embedded View

In this demo, let us see how to embed controls within a view and apply constraints to the embedded View instead of applying to the individual constraints.

Note :- Button has been incorrect named as Password it needs to be Register. The source code has been updated with this name change.

Add 2 textfields, 2 buttons and provide name and placeholder text as shown below.

Select these controls and embed them inside a View by navigating to Editor menu -> Embed -> View

Now change the background colour of the View and buttons to dark gray and orange.

Select the Embedded View, click Align option and make the View centre align to both Horizontal and Vertical to the Container.

Also add the width and height constraints for the Embedded View keeping the current value.

Now you might see a dashed orange colour line indicating the view is in misplaced position. You can fix this by selecting Update Frame under Resolve Auto Layout Issues option. Please make sure to select Update Frame option under Selected Views and not the container

Previewing the screen in both portrait and landscape should look as shown below

We have seen an introduction to Auto Layout using Storyboard. You can also add constraints via code using NSLayoutConstraint API or Visual Formatting language.

If you need any assistance in Auto Layout, check out our new iOS 9 Auto Layout Tutorials.

Download the source from GitHub

Filed Under: Auto Layout, Programming, Xcode

How to search, print and unhide comments in Excel

July 24, 2015 By Ravi Shankar 2 Comments

Adding a comment to a cell in Microsoft Excel is very useful feature. You can always locate a comment by red arrow appearing at the top right corner of the cell.

Comments in Excel 2013 and Excel 2010

List below are the steps to search, print and unhide comments in Excel 2013 and Excel 2010.

Search comment in Excel 2013 and Excel 2010

But searching and finding inserted comments in a excel worksheet with number of rows spanning across more than a page will be a huge task. This can be simplified using the Search Comments option available as part of the Find feature. To find comments in Excel 2010 and Excel 2013, navigate to Editing section and click the drop down arrow available next to Find & Select option.

Find option in Excel 2013 and Excel 2010

This would display the following menu options as part of the Find & Select feature.

Find comments in Excel 2013 and Excel 2010

Click Comments from the list of available menus. This would locate the comment  in your Excel Worksheet. And if you have more than one comment, then press tab to navigate to each comment.

Print cell comment in Excel 2013 and Excel 2010

Excel by default does not print the comments and you can enable this feature using the Page Setup option.

Let us added the following comment for a cell in Excel Worksheet.

Print comments in Excel 2010 and Excel 2013

Now when you navigate to Print Preview option (File –> Print), Excel will not show the added comments. Click the Page Setup option available under Print Preview Settings page.

Page setup option in Excel 2013 and Excel 2010

In the Page Setup Window, click the Sheet tab and navigate to Print section. For comments drop down, choose “At end of sheet” or “As displayed on sheet” for Comments. Let us go with “At end of sheet”, click Ok button to confirm and save the changes.

image

Now the print preview screen will display the comments at the end of sheet and the same will get printed while printing the worksheet.

Print preview comments in Excel 2013 and Excel 2010

Unhide comment in Excel 2013 and Excel 2010

If you are not able see the comments in worksheet then you can try to use the unhide option available as part of the Excel Advanced Options.

  1. Click the File menu and then the Options link.
  2. In the Excel Option window, navigate to Advanced Options.
  3. Scroll down to display section and For cells with comments radio option select your preferred choice i.e either Indicators only, and comments on hover or Comments and indicators.

Unhide comments in Excel 2010 and Excel 2013

Click Ok to save and confirm the changes.

Also See: How to Insert, Delete, Customize Comments in Excel 2010

Filed Under: Excel, Excel 2010, Excel 2013, MS Office Tagged With: Excel 2010, find comments, navigate, print comments, search, unhide comments

How to use Outlook as a RSS reader

July 23, 2015 By Ravi Shankar 9 Comments

There are lot of RSS readers available but if you have already installed Microsoft Outlook (Outlook 2013, Outlook 2010 and Outlook 2007) then you can use that as a RSS reader. Listed below are the steps to add and read RSS feeds In Outlook 2013, Outlook 2010 and Outlook 2007

In Outlook 2013 and Outlook 2010, Click the File menu –> Info and click on the Account Settings

In Outlook 2007, Click the Tools menu and select Account Settings

Account Settings in Outlook

Navigate to RSS feeds tab and click on New link

Add RSS feeds in Outlook 2013, Outlook 2010, Outlook 2007

Enter the RSS feed of a blog or a website. In this example, I am going to enter this tech blog RSS feed.

image

Click on the Add button to add the RSS feed. The following RSS Feed Options dialog box would be displayed and if you want to change the delivery location of the RSS feed then click on the Change Folder button and specify the desired location. Click Ok to confirm the changes.

Configure RSS feeds options in Outlook

Navigate to RSS Feed delivery location, this would display the RSS feeds from the configured tech blog

Outlook 2013, Outlook 2010 and Outlook 2007 as RSS Reader

How to share RSS feeds in Outlook 2013, Outlook 2010 and Outlook 2007

If you want to quickly mail the configured RSS feed to your email contacts then you can follow the below mentioned steps.

You can share the feed using the Share This Feed option available as part of Context menu or using Office Ribbon

Context menu – Right click on the any one feed post and select Share This feed option from the menu list.

Conext Menu Share This Feed

Office Ribbon – Select the feed that you want to share then navigate to RSS section under Home menu and click the Share This Feed option.

Office Ribbon - Share This Feed

This would launch the following Share RSS Feed window where you can enter the recipients email address and email them the feed details.

Share This Feed Window in Outlook 2010

Also See: How to configure RSS feeds from YouTube channel in Outlook 2013

Filed Under: MS Office, Office 2007, Office 2010, Office 2013, Outlook 2007, Outlook 2010, Outlook 2013 Tagged With: Configure RSS Feed, Outlook 2010, RSS Feed, RSS Reader, Share RSS feed

How to Insert, Delete, Customize Comments in Excel

July 22, 2015 By Ravi Shankar 8 Comments

This tutorial provides information on the comment feature available in Excel 2013 & Excel 2010. The following will be covered as part of this tutorial.

  • How to insert comment
  • Customize the comment box
  • Navigate between Comments
  • Delete Comment

How to insert comment

Excel allows users to insert comment using the options available as part of the Review menu. But if you want to quickly insert then right click on the cell and select Insert Comment from the listed menus.

image

image

 

Customize Comment

A typical comment box in Excel is as shown below.

image

In order to change the name that appears on the Comment box, click on the File menu then the Option link then in the General tab, navigate to Personalize your copy if Microsoft Office and enter your desired name in the User name box. Click OK button to save the changes. Now the entered name will appear when you Insert new Comments.

image

Similarly if you want to change the Font Style used in Comment box then select then right click on the Comment box and select Format Comment menu option.

image

The Format Comment window allows you change the Comments Font Style.

image

Navigate between Comments

If you have more than one comment inserted in the Excel worksheet and you want to navigate between the comments then you can use the menu options available as part of the Comment section under Review menu. The list option includes Show All Comments, Previous and Next.

image

Delete Comment

A Comment can be quickly deleted are hidden by right clicking on the cell and selecting the required menu option. You can also use the menu option available as part of the Comments section under Review menu.

image

image

By this way you can Insert, customise, navigate and delete comments in Excel 2013 and Excel 2010

Filed Under: Excel, Excel 2010, Office 2010 Tagged With: comments, Cutomize, Delete, Excel 2010, hide, Insert, navigate, Office 2010, Show

  • Go to page 1
  • Go to page 2
  • Go to page 3
  • Go to Next Page »

Primary Sidebar

Recent Posts

  • We have blocked all requests from this device – Firebase Phone Authentication
  • iPhone is not available error message in Xcode
  • Clear CocoaPods cache, re-download and reinstall all pods
  • PDFKit – View, Annotate PDF file in Swift
  • Tab Bar Controller with WebView

Archives

  • September 2020
  • April 2020
  • December 2019
  • November 2019
  • October 2019
  • February 2019
  • October 2017
  • June 2017
  • May 2017
  • March 2017
  • September 2016
  • March 2016
  • February 2016
  • January 2016
  • December 2015
  • November 2015
  • October 2015
  • September 2015
  • August 2015
  • July 2015
  • June 2015
  • May 2015
  • April 2015
  • March 2015
  • February 2015
  • January 2015
  • December 2014
  • November 2014
  • October 2014
  • September 2014
  • August 2014
  • July 2014
  • June 2014
  • April 2014
  • March 2014
  • February 2014
  • January 2014
  • December 2013
  • October 2013
  • August 2013
  • July 2013
  • June 2013
  • April 2013
  • March 2013
  • February 2013
  • January 2013
  • November 2012
  • August 2012
  • July 2012
  • June 2012
  • May 2012
  • March 2012
  • February 2012
  • January 2012
  • December 2011
  • October 2011
  • September 2011
  • August 2011
  • July 2011
  • June 2011
  • April 2011
  • March 2011
  • January 2011
  • September 2010
  • August 2010
  • July 2010
  • June 2010
  • July 2009
  • March 2008

Copyright 2020 © rshankar.com