I have a horizontal scroll view which contains 20 images. At the moment I have set all the images one by one and created an image view 20 times.
Something like this 20 times:
let imageView1 = UIImageView(frame: CGRect(x: calculateWidth(), y: 0, width: 60, height: 60))
imageView1.image = photos[0]
self.imageScrollView.addSubView(imageView1)
This look horrible. Can I optimize this in any way?
You seem to have an array or photos, why not just iterate through that array and create the imageViews. Something like this:
for photo in photos {
let imageView = UIImageView(frame: CGRect(x: calculateWidth(), y: 0, width: 60, height: 60))
imageView.image = photo
self.imageScrollView.addSubView(imageView)
}
You can iterate through your photos using a for loop, adding the images accordingly and changing the frame with every iteration:
var frame = CGRect(x: calculateWidth(), y: 0, width: 60, height: 60)
for photo in photos {
let imageView = UIImageView(frame: frame)
imageView.image = photo
self.imageScrollView.addSubView(imageView)
//update frame here for the next iteration
frame.origin.y += 60
}
If your coordinates follow no specific pattern, you can put them in an array and set them accordingly.
Nonetheless, depending on what you are trying to accomplish with the images, if you are trying to list these images or have them in a table or grid-like form, I suggest using a UITableView or a UICollectionView as these reuse cells and have much better performance than just adding many images on screen.
Related
I have looked at every example I can find in previous questions and nothing works. I am merging two images by copying a cropped image to pasteboard and then pasting it in another image view. That works fine but when I try to save it the image is always squished(may be a scale issue but it seems only the vertical size getting distorted.
This is the result of combining the images before trying to save it
This is after saving the image to the photos album
The image will not stay in the translated location and it's distorted.
Here is the code when I save the image
let fullSize:CGSize = photoView.image!.size
let newSize:CGSize = fullSize
let scale:CGFloat = newSize.height/fullSize.height
let offset:CGFloat = (newSize.width - fullSize.width*scale)/2
let offsetRect:CGRect = CGRect.init(x: offset, y: 0, width: newSize.width - offset*2, height: newSize.height)
UIGraphicsBeginImageContext(newSize);
self.photoView.image!.draw(in: offsetRect)
self.copiedView.image!.draw(in: CGRect.init(x: 0, y: 0, width: photoView.image!.size.width, height:photoView.image!.size.height))
let combImage:UIImage = UIGraphicsGetImageFromCurrentImageContext()!
UIGraphicsEndImageContext();
//photoView.image = combImage
UIImageWriteToSavedPhotosAlbum(combImage, self, #selector(image(_:didFinishSavingWithError:contextInfo:)), nil)
Any help is appreciated
i have created a subview with a lot sliders in it
var sliderArea = UIView()
sliderArea = UIView(frame: CGRect(x: 300, y: 400, width: 500, height: 100)
view.addSubview(sliderArea)
mySlider1 = setUpSlider(sliderNr: 1, ypos: 30)
mySlider2 = setUpSlider(sliderNr: 2, ypos: 30)
mySlider3 = setUpSlider(sliderNr: 3, ypos: 30)
sliderArea.addSubview(mySlider1)
sliderArea.addSubview(mySlider2)
sliderArea.addSubview(mySlider3)
i have a lot of subviews similar to the "sliderArea" to be able to change my sliders quickly while the layout adopts automatically
now i need to know where the absolute position of each slider is to place buttons on top of it. i need to have all this buttons inside an extra view on top of it all. any ideas? thank you
You should convert your slider frame to window's coordinate system using this method of CGRect:
https://developer.apple.com/documentation/uikit/uiview/1622504-convert
Just pass nil as second parameter and it will return you an absolute frame.
I need to create custom button class, reuse it 4 times and I also need to override its text and image name. My next problem is how to set its frame somehow dynamically (now it is static), because I need this 4 buttons in grid 2x2.
I'm trying to create button exactly like this: https://imgur.com/a/dNhUGhc.
I have coded this but it is static and in ViewController I can't edit (override) these labels and image name. And if I tried to reuse this class I would have them in the same spot, because frame settings is exactly the same.
I'm subclassing UIButton. If something more suitable exists just let me know.
Code for adding label
// city label
let cityRect = CGRect(x: 0, y: 20, width: buttonWidth, height: 25)
let cityLabel = UILabel(frame: cityRect)
cityLabel.text = "Label"
cityLabel.font = UIFont.systemFont(ofSize: 17, weight: .semibold)
cityLabel.textAlignment = .center
addSubview(cityLabel)
Code for adding image
// image
let imageView = UIImageView(image: UIImage(named: "something"))
imageView.frame = CGRect(x: 0, y: 60, width: 40, height: 40)
imageView.center.x = self.center.x - 20
addSubview(imageView)
Can you guys help me? Thanks
It looks like what you need to do is use an IBOutlet. Basically, an IBOutlet will give you a reference within your code (custom UIView or UIViewController subclass) to the button that you've setup in xib or storyboard. Then you can make any changes or adjustments that you want to it at runtime.
Check this out to learn more about IBOutlets and how to set them up in your project.
In my swift app I've a collection view and I want to creare a stretchable header view like this in table view: https://medium.com/if-let-swift-programming/how-to-create-a-stretchable-tableviewheader-in-ios-ee9ed049aba3
You already answered your question yourself with that article link, unless I miss something.
I will copy & paste for you and others that may have the same question, if it helps, because it even ships with a github link (kudos to Abhimuralidharan # Medium):
Create a tableview with the basic datasource and delegate methods which are required to simply load the table with some data.
Set the tableview’s contentInset property:
tableView.contentInset = UIEdgeInsetsMake(300, 0, 0, 0)
Here, I set the top value as 300 which is a calculated number which I will set as the initial normal height for the header imageview. Now, that we set the contentInset , the tableview’s frame will start at (0,0) and the first cell will start at (0,300).
Now, create an imageview with height 300 and add it to the current View above the tableview.
imageView.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: 300)
imageView.image = UIImage.init(named: “poster”)
imageView.contentMode = .scaleAspectFill
imageView.clipsToBounds = true
view.addSubview(imageView)
Then, add the following code in the scrollview delegate method scrollViewDidScroll which gets called every time the tableview is scrolled.
func scrollViewDidScroll (_ scrollView: UIScrollView) {
let y = 300 — (scrollView.contentOffset.y + 300)
let height = min(max(y, 60), 400)
imageView.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: height)
}
Compile and run the code. Full source code is available in github.
Supposedly, the twitter login button is a subclass of UIButton but none of the methods associated with setting a UIButton(such as setTitle(), setBackgroundColor() etc) object seem to work with the twitter button. Specifically, I'm trying to resize it but nothing seems to work. Here's what I've tried so far in my viewDidLoad()
let twitterLoginButton = TWTRLogInButton(frame: CGRectMake(0, 0, 200, 50))
self.view.addSubview(twitterLoginButton)
I also tried this
let twitterLoginButton = TWTRLogInButton.init(frame: CGRectMake(0, 0, 200, 50))
self.view.addSubview(twitterLoginButton)
Then I tried to create a container UIView for the login button with the above rect parameters and then place the button into it, instead of directly in the UIView controller like so
let twitterLoginButton = TWTRLogInButton()
let twitterLoginButtonParent = TWTRLogInButton(frame: CGRectMake(0, 0, 200, 50))
twitterLoginButtonParent.addSubview(twitterLoginButton)
self.view.addSubview(twitterLoginButtonParent)
let twitterLoginButtonSize = CGSize(width: 200, height: 50)
twitterLoginButton.sizeThatFits(twitterLoginButtonSize)
At least this time, the button doesn't get stuck in the top left hand corner but is forced to assume the x and y positions of its new parent. However it still cant be forced to assume the size of its parent as I intended.
Is there any way the button can be forced to resize? With the facebook button, it's much easier.
This solved it. Apparently I had to first initialize the button with loginCompletion
let twitterLoginButton = TWTRLogInButton(logInCompletion: { session, error in
if (session != nil) {
print("signed in as \(session!.userName)");
} else {
print("error: \(error!.localizedDescription)");
}
})
twitterLoginButton.frame = CGRectMake(0, 0, 200, 50)