Comments (11)
Hi, one can use CosmosView like any other UIView from code. Here is an example that shows how to create a cosmos view and position it with auto layout constraints.
Here is a sample project: UsingCosmosWithoutStoryboard.zip.
func createCosmosView() {
let cosmosView = CosmosView()
view.addSubview(cosmosView)
addLayoutConstrains(cosmosView)
}
func addLayoutConstrains(view: UIView) {
guard let superview = view.superview else { return }
view.translatesAutoresizingMaskIntoConstraints = false
var constraints = [NSLayoutConstraint]()
// Center view horizontally
// --------------
constraints.append(
NSLayoutConstraint(item: view,
attribute: .CenterX,
relatedBy: .Equal,
toItem: superview,
attribute: .CenterX,
multiplier: 1,
constant: 0)
)
// Align the view's top with the top of its superview
// --------------
constraints.append(
NSLayoutConstraint(item: view,
attribute: .Top,
relatedBy: .Equal,
toItem: superview,
attribute: .Top,
multiplier: 1,
constant: 100)
)
NSLayoutConstraint.activateConstraints(constraints)
}
Alternatively, if you need to have a collection of cosmos views you can use a table view and put CosmosView in a table view cell from the storyboard. There is another demo app in this Github repository that has a "Performance" button. The button shows a screen with a very long list of cosmos views in a table view.
Let me know how it goes.
from cosmos.
A couple of things about this thread (which helped me get Cosmos running fast - thanks!)
-
If you are having trouble with 'import Cosmos' not being recognized by xcode, do a 'Clean' (that seems to fix a world of woes!) - "Product => Clean"
-
for Swift 3, the code is slightly different (so including it here - this works instantly to put the stars on the screen....
import Cosmos
class RateARideViewController: UIViewController{
func createCosmosView() {
let cosmosView = CosmosView()
view.addSubview(cosmosView)
addLayoutConstrains(view: cosmosView)
}
func addLayoutConstrains(view: UIView) {
guard let superview = view.superview else { return }
view.translatesAutoresizingMaskIntoConstraints = false
var constraints = [NSLayoutConstraint]()
// Center view horizontally
// --------------
constraints.append(
NSLayoutConstraint(item: view,
attribute: .centerX,
relatedBy: .equal,
toItem: superview,
attribute: .centerX,
multiplier: 1,
constant: 0)
)
// Align the view's top with the top of its superview
// --------------
constraints.append(
NSLayoutConstraint(item: view,
attribute: .top,
relatedBy: .equal,
toItem: superview,
attribute: .top,
multiplier: 1,
constant: 100)
)
NSLayoutConstraint.activate(constraints)
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
createCosmosView()
}
override func viewDidLoad() {
super.viewDidLoad()
}
}
(sorry, I can't seem to get this to format..... :( - but the code works, first time.... 👍
- I respectfully suggest that there should be a link perhaps only to this thread?) and/or more clear instructions on the main page of what "instantiate CosmosView class and add it to the view manually " means (for those of us moderately new to xcode....)
from cosmos.
Dear Evgeny
Thank you very much for your reply. I found the reason why I couldn't make it work: If you load Cosmos using pods you cannot create a CosmosView(). If you import Cosmos it doesn't seem to work either.
I copied the CosmosDistrib.swift file from your sample project and now it works wonderfully. Thank you very much!
from cosmos.
Dear Evgeny
Thank you very much for your advice. I ended up doing a tableview. But how can I collect the information about the rating? I put a didFinishTouchingCosmos function inside of the TableViewCell, but it doesn't get called. What did I do wrong? Thank you very much in advance!
Best regards,
Andrei
from cosmos.
I saw that you wrote:
"To collect rating from the user and send it to your server you can use didFinishTouchingCosmos property of cosmos view, there is example in the readme. You can assign a function to it after getting your cell view with dequeueReusableCellWithIdentifier function." But I do not understand how to put this in code. Excuse me, I just started writing my app a few weeks ago and would be very glad if you could help me.
Best regards,
from cosmos.
@andreichirkunovgoon, if you are getting user's input from a cosmos view in a table view cell there may be weird touch problems because the table view itself is scrolled by touching. But if you are happy to try it anyway here is how to do it. In the demo app of this repository there is class called PerformanceTableViewCell. It has an update
method that is called when the cell is dequeued in PerformanceTableViewController. In this update
method you can assign a closure or a function. For example:
public class PerformanceTableViewCell: UITableViewCell {
@IBOutlet var cosmosView: CosmosView!
func update(rating: Double) {
cosmosView.rating = rating
cosmosView.didTouchCosmos = { rating in
// Example of using a closure
}
cosmosView.didFinishTouchingCosmos = myFunction // Example of using a function
}
func myFunction(rating: Double) {
cosmosView.backgroundColor = UIColor.redColor()
}
}
Please beware that didFinishTouchingCosmos may not always trigger when the cosmos view is in the table view.
from cosmos.
Thanks for your tips, @TCOA. You are right, I should probably add a separate Wiki about this.
from cosmos.
@evgenyneu - could you please 'translate' the constraint code to help me get the stars where I want them on the screen? (super-newbie rating showing here! :)
When I use a separate view controller (as shown above), everything is great. But, now I have been asked to put the stars on top of another viewController and I can't figure out how to line them up.
What I am looking for is a way to place the frame where/as I need it programmatically, but then also have the CosmosView features - something like:
let cosmosView = CosmosView(frame: CGRect(origin: CGPoint(x: 0, y: 100), size: CGSize(width: self.view.frame.width, height: 50)))
viewController.view.addSubview(cosmosView)
from cosmos.
@TCOA, your code where you create cosmos view with frame should work. Is there a problem?
from cosmos.
The problem was that I was mixing "self' and 'viewController' sizes and things weren't going where I wanted so I thought I was just doing it wrong.....
Here's the final I wound up with and it works great (makes this great library even more powerful! I will suggest when you update a Wiki that you include this sort of thing as well - for those of us that don't use the constraints method to include the subviews - this allows you to put the stars anywhere you want in a 'one liner')
let cosmosView = CosmosView(frame: CGRect(origin: CGPoint(x: viewController.view.frame.width / 4, y: 100), size: CGSize(width: viewController.view.frame.width / 2, height: 50)))
from cosmos.
I'm glad the problem is fixed. Thanks for your suggestion.
from cosmos.
Related Issues (20)
- Unwanted behavior in a UIScrollView HOT 4
- Please add macOS support in SwiftUI HOT 3
- How to disable tapping? HOT 2
- How to set empty image for empty start(non partially filled as well) HOT 6
- My image and `withTintColor` HOT 2
- How to prevent didTouchCosmos from issuing a rating
- Can you please build the framework for the Simulator HOT 3
- [Feature Request] Add custom image for half star HOT 4
- Filled color values changing even though the data is not. HOT 6
- Error While installing pod. HOT 1
- Tap gesture recognizer messes up Cosmos HOT 1
- Build failed in Storyboard
- Showing Recent Messages /Package.swift has no Package.swift manifest for version 9.0.0 in https://github.com/evgenyneu/Cosmos.git
- Single star doesn't show precise filling.
- Tapping on Rating always select 3 rating when VoiceOver is on
- Remove rating after giving
- Set custom accessibilityLabel for Cosmos View HOT 1
- demo
- Privacy Manifests HOT 21
- I have error when run demo project
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from cosmos.