How to define a class and module for a view created programmatically in swift - ios

Let's say I create a view like this:
let myView = UIView(frame: CGRect(x: 0, y: 0, width: 50, height: 50))
self.view.addSubview(myView)
How can I give that view a custom class and module like I would in IB?
I am either searching for the wrong thing, or no one has asked this question. I also haven't tried anything because I can't figure out where to even start.
Update:
This is what I mean by adding a class and module like you would in IB:

Simply instantiate it instead of UIView.
Assuming this is your custom view:
class MyCustomView: UIView {
//...
}
Here is how to instantiate it:
let myView = MyCustomView(frame: CGRect(x: 0, y: 0, width: 50, height: 50))
self.view.addSubview(myView)
The module is the module your source file of class MyCustomView: UIView... is member of. When developing an iOS application (not a framework or other target types) this is your app. You can choose the "Target Membership" in Xcodes Inspector when selecting a source file:

Related

iOS: AutoLayout a dynamic number of items

I'm trying to recreate the view shown below. The difficulty for me is that some of these elements will have a different number of instances.
First item is a UILabel with variabel multi lines. There will always be axactly one of this item.
UIImageView. This there can be zero or one item of.
UIButton zero or more (at least up to 3-4).
How to achieve this?
What would be a best practice to achieve this? I had one idea to use UITableView and just put each element in a table view cell. But it feels like a bit hacky solution. Especially since I then have to remove the default styling of the table (borders and padding).
Another solution is to maybe use a UICollectionView with only one column.
Third solution is to build on the autolayout solution I have, and achieve it with a lot of if statements and for-loops. This seems like a bad solution.
So how would I achieve this (using best practices)? And is it generally a bad idea for instance to use UITableView for pure layout purposes?
Create a UIStackView and inside it another one for every item with zero or more instances
Add when you want to append say a button to the buttons stackView use this
btnsStackView.addArrangedSubview(btn)
same for UILabels and UIImageViews
Check this it may help
for i in 0...5
{
let headerView:UIView = UIView(frame: CGRect(x: 0, y: 0, width: self.view.bounds.size.width, height: self.view.bounds.size.height))
if(i % 2 == 0)
{
headerView.backgroundColor = UIColor.blue
}
else
{
headerView.backgroundColor = UIColor.yellow
}
self.stttq.addArrangedSubview(headerView)
}
let headerView:UIView = UIView(frame: CGRect(x: 0, y: 0, width: self.view.bounds.size.width, height: self.view.bounds.size.height))
headerView.backgroundColor = UIColor.black
self.stttq.insertArrangedSubview(headerView , at:0)

Is it possible to use custom iOS UI elements like UILabel in augmented reality app

I am wondering if I can use UI elements like UIButton, UILabel in an augmented reality app with ARKit.
If you are also interested in transparency modes for that UIView subclasses try my sample https://github.com/erikhric/ar-menu
You can use different blending modes. I guess .alpha will work for your purposes.
Yes, you can use UIKit elements by adding them to a UIView that's positioned above the view displaying the AR scene (ARSKView or ARSCNView).
If you create a new project in Xcode and select the "Augmented Reality App" template, you can see that the AR content is just a view like any other UIKit view.
What worked best for me
in main.storyboard:
- delete SceneView
- add regular UIView
- add ARKit SceneKit View on top of that
- then you can add buttons, etc.
Yes you can place UI elements on top of the ARSKView or ARSCNView displaying the AR scene:
let scanningPanel = UIImageView()
scanningPanel.backgroundColor = UIColor(white: 0.33, alpha: 0.6)
scanningPanel.layer.masksToBounds = true
scanningPanel.frame = CGRect(x: -2,
y: self.sceneView.frame.height-270,
width: 178,
height: 50)
scanningPanel.layer.cornerRadius = 10
let scanInfo = UILabel(frame: CGRect(x: 8,
y: self.sceneView.frame.height-268,
width: 160,
height: 45))
scanInfo.textAlignment = .left
scanInfo.font = scanInfo.font.withSize(15)
scanInfo.textColor = UIColor.white
scanInfo.text = "SCAN A SURFACE"
Adding:
self.sceneView.addSubview(scanningPanel)
self.sceneView.addSubview(scanInfo)
Removing:
if(scanInfo.isDescendant(of: self.sceneView)) {
scanInfo.removeFromSuperview()
}
You can insert content of any view on a plane in ARKit like this:
let plane = SCNPlane(width: sceneView.bounds.width/3000,
height: sceneView.bounds.height/3000)
plane.firstMaterial?.diffuse.contents = self.anyView`
Gestures and taps are automatically sent to that view.
Try my example.

iOS - Location of a TextField created programmatically

I have a doubt: it is wrong to create a TextField from the code and not from interface builder?
For example, I am using 'AwsomeTextField' taken by CocoaPods.
In the pages of explanation says:
let field = AwsomeTextField(frame: CGRect(x: 60, y: 200, width: 320, height: 44))
field.placeholder = "test"
view.addSubview(field)
But if I assign an absolute position with CGRect (x: 60, y: 200, width: 320, height: 44) then if I run the app on an iPad or an iPhone IF of course the graphics becomes atrocious.
I can use the modules of cocoa pod from interface builder to add the right constraints, or can I work on autolayout also by code? I'm confused!
There is nothing to stop you adding constraints to a view created in code:
let field = AwsomeTextField(frame: .zero) // Create the view with a zero frame
field.placeholder = "test"
field.translatesAutoresizingMaskIntoConstraints = false // You should always do this to views you create in code that you want to apply layout constraints to
view.addSubview(field)
// No that you have added field as a subview, you can add constraints to it here.
If you want to use Interface Builder, drag a plain UIView from the picker onto your view controller and add any constraints. Then, set the class of the view from UIView to AwesomeTextField
'AwsomeTextField' Pod is an extension of a UITextField.
Drag a UITextField object in interface builder, make an outlet and switch the outlet type to AwesomeTextField like that:
#IBOutlet weak var awesomeTextField: AwsomeTextField!
also include AwsomeTextField class in Custom Class section of the Identity Inspector in xCode.
This way you don't need to create it from code all the time ;]

Swift - SWRevealViewController menu placed after the status bar

I'm using SWRevealViewController menu and I have a header view for the table. You can see the table controller view structure and constrains:
And this is how my menu looks like
but I need it to be like
I tried :
self.profileHeader.frame = CGRect(x: 0, y: -20, width: profileHeader.frame.width, height: profileHeader.frame.height + 20)
But nothing changed.
Try using a normal UIViewController, and adjust constraints for the tableView manually check top constraint linked to view and not to top layout guide.
I hope this helps you, let me know about

iOS dynamic UI in simple terms?

This may be too basic or require rephrasing. I am in the process of learning Swift and iOS programming and have developed a basic application that runs successfully on my iPhone 5. The app consists of a label, a button, and a UIImageView. It looks the way I want it to on my iPhone 5.
I figured most of this out by just playing around and so I am creating all these elements programatically. The code looks like this:
let banner = UILabel(frame: CGRect(x: 10, y: 10, width: 300.0, height: 75.0))
let button = UIButton(frame: CGRect(x: 45, y: 75, width: 235.0, height: 60.0))
let imageView = UIImageView(frame: CGRect(x: 22, y: 150, width: 280.0, height: 410.0))
And then I configure them in viewDidLoad to make them show stuff.
Now the question...how to I make them all the right size when running on different devices? I can load up the app on my iPad Mini but it's all scrunched over to the left of the view. So I need to do some kind of dynamic layout but not sure where to start.
All help appreciated!
Rather than creating the view sizes explicitly with initWithFrame: constructors, you can programmatically create and NSLayoutConstraints to your views to automatically layout your views, the same as if you used Auto Layout with the Interface Builder. See Apple's Auto Layout Guide for more details.

Resources