Reputation: 9219
I am a newbie in iOS Development.
I need to create a view which is quite complex in layout and I need to do create it programatically only.
There are labels, scrollviews, etc. in that view.
Right now in order to set the labels properly, I need to run simulator lot many times. This is slowing my development process.
I want to know if there is a way to easily set the labels/view without running simulator continuously, when we are creating views via code (no XIB).
I would really appreciate if someone can point me in the right direction.
Thank you!
Upvotes: 0
Views: 240
Reputation: 398
Since you are not using the interface builder to make your view, you will have to compile and run to see them in action,but don't worry, and even the interface builder preview can generate different results compared to the simulator and the device.
Also, if there is other solutions feel free to update the answer.
Upvotes: 0
Reputation: 4658
I use playgrounds when I want to rapidly prototype a view class. When using the view preview functionality you can create a view with whatever frame you want to display on. Here's a very basic example view being displayed on a iPhone 5s screen:
//: Playground - noun: a place where people can play
import Foundation
import UIKit
class MainView : UIView {
var contentContainer : UIScrollView
var header : UILabel
var content : UILabel
var image : UIImageView
override init(frame: CGRect) {
contentContainer = UIScrollView(frame: CGRect(origin: CGPoint.zero, size: frame.size))
contentContainer.backgroundColor = UIColor.whiteColor()
header = UILabel(frame: CGRect(x: 10, y: 10, width: frame.width-20, height: 30))
header.font = UIFont.systemFontOfSize(20, weight: UIFontWeightBold)
header.text = "MY VIEW HEADER"
header.textAlignment = NSTextAlignment.Center
header.textColor = UIColor.grayColor()
contentContainer.addSubview(header)
image = UIImageView(frame: CGRectMake(0, 0, 200, 100))
image.center = CGPoint(x: frame.width/2, y: header.frame.origin.y + header.frame.size.height + 10 + image.frame.size.height/2)
image.backgroundColor = UIColor.orangeColor()
contentContainer.addSubview(image)
content = UILabel(frame: CGRect(x: 20, y: image.frame.origin.y + image.frame.size.height + 20, width: frame.width-40, height: 110))
content.text = "Bacon ipsum dolor amet flank kielbasa drumstick, ham tongue pancetta shank. Shankle tenderloin filet mignon andouille doner short ribs meatball frankfurter. Ham boudin tri-tip porchetta fatback, bresaola landjaeger kielbasa brisket pork belly bacon picanha alcatra ham hock. Venison turducken boudin pork loin meatloaf spare ribs meatball biltong rump t-bone bacon ground round leberkas filet mignon. Shankle meatloaf ham hock strip steak porchetta."
content.numberOfLines = 0
content.font = UIFont.systemFontOfSize(10, weight: UIFontWeightLight)
contentContainer.addSubview(content)
contentContainer.contentSize = CGSizeMake(contentContainer.frame.width, content.frame.origin.y + content.frame.height + 50)
super.init(frame: frame)
self.addSubview(contentContainer)
}
//needed to remove that annoying warning
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
//now preview the resulting view by clicking the dot on the left side -->
let view = MainView(frame: CGRect(x: 0, y: 0, width: 320, height: 568))
Hope this helps!
Upvotes: 1
Reputation: 11201
If you dont want to use simulator, then you have no other option other than using the Interface Builder. By using the interface builder, you can make the custom views IB_DESIGNABLE
, so that you can just build them on the Interface Builder.
Upvotes: 0
Reputation: 132
Xcode playground mode may help you (UIKit supports).
From Apple Doc:
*A playground is an interactive Swift coding environment that evaluates each statement and displays results as updates are made, without the need to create a project.
Use playgrounds to learn and explore Swift, prototype parts of your app, and create learning environments for others. The interactive Swift environment lets you experiment with algorithms, explore system APIs, and even create custom views.
Share your learning with others by adding notes and guidance using rich comments. Create an explorable learning environment by grouping related concepts into pages and adding navigation.*
Upvotes: 0