My problem is that based on the number of groups a person has I want to dynamically create that many UIImageView and assign them to the view. I know how to create a single UIImageView and put it onto the view but how can I create a variable number of them since creating another with the same name seems to just overwrite the original? I tried sending the UIImageView into an Array but got an optional error.
circleView = UIImageView(frame:CGRectMake(125, y, 75, 75))
y+=150
circleView.image = UIImage(named:"circle.png")
circleView.tag = i
self.view.addSubview(circleView)
circleArray[i] = circleView
The problem is that you keep reusing the same circleView instance. When you add it to the array, you are just adding a reference to it. You then reinitialize it, which wipes it out. I also don't see where you are incrementing i. Try something like this:
var circleArray = Array<UIImageView>()
var y : CGFloat = 0.0
var i = 0
circleArray.append(UIImageView(frame:CGRectMake(125, y, 75, 75)))
var circleView = circleArray[i]
circleView.image = UIImage(named:"circle.png")
circleView.tag = i
self.view.addSubview(circleView)
y+=150
i += 1
You need to use circleArray.append(circleView)
Related
var mainStack = new UIStackView();
mainStack.Alignment = UIStackViewAlignment.Leading;
mainStack.Spacing = 10;
mainStack.Axis = UILayoutConstraintAxis.Vertical;
mainStack.Frame = new CGRect(10,10,Frame.Width - 30,Frame.Height - 100);
var mainStack1 = new UIStackView();
mainStack1.Alignment = UIStackViewAlignment.Center;
mainStack1.Spacing = 10;
mainStack1.Axis = UILayoutConstraintAxis.Vertical;
mainStack1.Frame = new CGRect(5, 5, Frame.Width - 30, Frame.Height - 200);
this.AddSubview(mainStack);
this.AddSubview(mainStack1);
These are the two subviews i have used, I want to insert one subview below to another subview. I faced a problem that the two subviews are override.
The easiest thing is to change the order you add the views. The last one added will be on top, so if the issue is just with the exact code you shared, just reverse the order of adding the subviews:
this.AddSubview(mainStack1);
this.AddSubview(mainStack);
Also you can bring a subview to the front with (assuming this is a view):
this.BringSubviewToFront(mainStack);
Or you can adjust the Z-Order using the Layer property of a view:
mainStack1.Layer.ZPosition = 0;
mainStack.Layer.ZPosition = 1;
UIView.Layer with higher z positions will appear in front of UIView.Layer with lower z positions.
In the for loop I am trying to place images from arrayOfImages in rectangles that I've created with CGRect.
But after that I want to use class MyClassView which inherits UIView, because I have there additional functions that I want to apply on my rectangle images that I've just created, but I am getting the error
cannot assign value of type UIView to type CGRect
let widthRect = 50
func createMyRects(){
let insetSize = CGRectInset(self.view.bounds, CGFloat(Int(widthRect)), CGFloat(Int(widthRect))).size
for i in 0...arrayOfImages.count-1{
let pointX = CGFloat(UInt(arc4random() % UInt32(UInt(insetSize.width))))
let pointY = CGFloat(UInt(arc4random() % UInt32(UInt(insetSize.height))))
let showImages = UIImageView(image: arrayOfImages[i])
let newFrame = CGRect(x: pointX, y: pointY, width: CGFloat(widthRect), height: CGFloat(widthRect))
//showImages.frame = newFrame
let newFrame2 = MyClassView(frame: newFrame)
showImages.frame = newFrame2 //here it says: cannot assign value of type UIView view to type CGRect
view.addSubview(showImages)
}
}
To fix the error you need to assign the frame of your UIView subclass to the frame.
showImages.frame = newFrame2.frame
Your variable names are a bit misleading.
Justin gave an answer that would let your code compile, but it still doesn't make sense. I assume that the MyClassView(frame: newFrame) bit is an intializer for a MyClassView object? Why in the world would you create a new view object only to take it's frame and assign it to another view, and then discard the newly created view?
Why do you need to create a MyClassView object at all? My guess is that you don't, and that you shouldn't. Get rid of the line that creates a MyClassView object entirely and just use the code you have commented out? (showImages.frame = newFrame).
Finally I've solved it. Just wanted to post the answer if someone else comes up with same problem. Previously i tried to assign images, which i made from one picture with function that sliced original picture, to bunch of rectangles that i created. But it didn't work because i didn't correctly make my class MojView. It inherited UIView, now it inherits UIImageView. Besides, i made a change in one line of code and the result is - rectangles are assigned with images and all of them can be dragged which was my intention when i created MojClass. Here is the code for both MojClass and the rest of app. MojClass: https://github.com/Vukovi/Ne-to/blob/master/README.md && Rest: https://github.com/Vukovi/ostatak/blob/master/README.md
I am trying to implement , i cannot set number of views, i want to set only five view, which property i need to use before view is loading.
Library link enter link description here
self.swipeableView.numberOfActiveView = 5;
self.swipeableView.discardViews()
self.swipeableView.loadViews()
I am trying with their demo application.
You can Replace your colors array with other array like,
Declare one more array below that color array,
var imgArray = ["1","2","3","4"]
Now find the color array from the view did load and replace it with imgArray like,
swipeableView.nextView = {
if self.colorIndex < self.imgArray.count //self.colors.count
{
var cardFrame = CGRectMake(self.swipeableView.frame.origin.x, 0, self.swipeableView.frame.size.width, 250)
var cardView = CardView(frame: cardFrame)
var img : UIImageView = UIImageView(frame: cardView.bounds)
img.image = UIImage(named: self.imgArray[self.colorIndex])
cardView.addSubview(img)
self.colorIndex++
....
....
}
This working fine at my end so hope this will help you.
i am trying to set imageview as subview for scrollview.for that i am setting different frame depends upon the screensize.
This is my code
let screensize:CGRect=UIScreen.mainScreen().bounds
let screenwidth=screensize.width
var frames:CGRect
for var index=0 ;index < arrBirds.count ;index++
{
if(screenwidth==320)
{
frames.origin.x = CGFloat(index) * (self.view.frame.size.width);
frames.origin.y = 0;
frames.size=scrollview.frame.size;
}
else if(screenwidth==375)
{
frames.origin.x = CGFloat(index) * (self.view.frame.size.width);
frames.origin.y=0;
frames.size=scrollview.frame.size;
}
else
{
frames.origin.x = CGFloat(index) * (self.view.frame.size.width);
frames.origin.y=0;
frames.size=scrollview.frame.size;
}
imageView=UIImageView(frame:frames)
imageView.image=UIImage(named: arrBirds[index] as! String)
scrollview .addSubview(imageView)
But i am getting error this line
frames.origin.x = CGFloat(index) * (self.view.frame.size.width);//struct frame must be completely initialised before a member is stored to.
You have not yet created an instance of the struct CGRect yet. Before accessing / setting any members on it you you need to create a instance first and only after that set the different members of it:
Change the line var frames:CGRect to:
var frames = CGRectZero
According to Apple Docs :
Classes and structures must set all of their stored properties to an
appropriate initial value by the time an instance of that class or
structure is created.
Your frames object isn't initialized, instead you should write :
var frames = CGRectZero
I have about 80 CALayer properties I need to add to a UIViewController and I'm wondering if anyone knows a way to do this with a for loop as opposed to me copying and pasting 80 lines of code like this:
var colorButton1 = CALayer()
var colorButton2 = CALayer()
var colorButton1 = CALayer()
var colorButton2 = CALayer()
var colorButton1 = CALayer()
var colorButton2 = CALayer()
........(colorbuttons go to 60)
I know how to make the objects with a for loop in the viewDidLoad method, like this:
let colorLayers = 60
var colorLayer = CALayer()
var buttonYPosition:CGFloat = 0.0
for colorNum in 1...60 {
buttonYPosition = buttonYPosition + 50
colorLayer.contents = UIImage(contentsOfFile:NSBundle.mainBundle().resourcePath!.stringByAppendingPathComponent("color\(colorNum).png"))!.CGImage
colorLayer.frame = CGRectMake(device.x/2, buttonYPosition, worldScale * 380, worldScale * 180)
colorLayer.zPosition = 6.0
self.view.layer.addSublayer(colorLayer)
}
but these are not properties.
So how can I put something like this in the top of my script so it creates 60 properties with 60 different indexed names, as opposed to 60 objects with the same name?
I'm not even sure this is possible, I'm just hoping to condense my code.
When dealing with many objects of the same type, rather than declaring all of them separately, you should make an array of that object type. In the case of CALayer, you can initialize an array of 60 objects using the count:repeatedValue initializer like so:
var layers = [CALayer](count: 60, repeatedValue: CALayer())
override func viewDidLoad() {
super.viewDidLoad()
// Access all layers
for layer in layers {
// Do something
}
// Access an individual layer
layers[0].frame = CGRectZero
}
Notice you access individual layers using subscript notation, which is cleaner than doing layer1, layer2, layer3, etc...
Use an array of CALayers:
var colorLayers = [CALayer]()