Category: User Interface
If you’re creating a macOS program using storyboards, you may need to know the size of a window. To get this information, you just need to follow two steps:
- Create an IBOutlet that represents your window. This IBOutlet should be of the type NSView
- Use the frame.width and frame.height properties to get the width and height of the window defined by your IBOutlet
To create an IBOutlet for a window, just opens he Assistant Editor, move the mouse pointer over the window, and Control-drag to the Swift class file linked to your window such as ViewController.swift. Then release the Control key and the left mouse button to create an IBOutlet.
You can use any name for your IBOutlet, but whatever name you choose will now represent your window such as:
Assuming you named your window IBOutlet myWindow, you’ll be able to access the frame.width and frame.height properties like this:
If you put the following two lines of code in the viewDidLoad function that runs as soon as a window appears, you’ll see the defined width and height of the window.
Posted in Algorithms, User Interface
if you’ve ever used the appointment feature of the iOS calendar, you’ve seen a spinning dial that lets you choose among multiple options such as the names of different months and days. A PickerView simply displays options that the user can choose. To fill a PickerView with data, you can use an array and place that PickerView on the user interface.
Next, you have to add the UIPickerViewDelegate and UIPickerViewDataSource to the class linked to the view (user interface) like this:
Then you have to get data to place in the PickerView, which can be an array like this:
You also need to create an IBOutlet for your PickerView so you can refer to it in Swift code like this:
Once you’ve created an IBOutlet for your PickerView, you can then set its delegate to the view controller Swift file like this:
Now you need to define the number of parts or components in your PickerView. The simplest PickerView displays just one type of data so you can define just 1 using the numberOfComponentsInPickerView function:
You must also define how many items to display in the PickerView. If your data is an array, you can just count the number of items in the array using the pickerView function:
Finally, you need to actually fill the PickerView with your data using another pickerView function:
Your entire ViewController.swift file can look like this when linked to a view (user interface) that contains a picker view control:
Posted in iOS, User Interface
Your app can display a web page by using the Web View object on your user interface and then by writing three lines of code. So first, place a Web View on your user interface and give it a unique name such as webView, although the exact name isn’t important.
Next, create a variable or constant using any name you wish. Then set this variable/constant to a string that defines the complete web site address like this:
The only restriction is that the web site address must use https:// and not just plain http:// or else it won’t work.
After creating a variable/constant to represent the web site address, you need to create a second variable/constant to request the web site address, known as a URL:
Finally you need to use the loadRequest method on your Web View to request a particular web site domain address:
So the complete code to work with a Web View object on your user interface looks like this:
Just paste a different string for the web site address and you’ll be able to display a different web site. Remember, make sure the web site address uses https:// and also make sure the web site is optimized for mobile devices so it adapts to the smaller screen of an iPhone or iPad.
Posted in Programming, User Interface, Xcode