I have an array of png icons homeIcons[] that load into the cells of my tableView. Unfortunately, they are too big and want them to shrink a bit. Trying to shrink them with this code:
let cellIcon = UIImage(named: homeIcons[indexPath.row])
cell.imageView!.frame = CGRect(x: cell.imageView!.frame.origin.x, y: cell.imageView!.frame.origin.y, width: cell.imageView!.frame.size.width / 2, height: cell.imageView!.frame.size.height / 2)
cell.imageView!.image = cellIcon
Changing the frame width and height doesn't do anything. Is there anyway to change the size of my icons or am I forced to redraw my images?
Better if can add auto layout constraint to your imageview from storyboard.
or try calling layoutIfNeeded() after setting new frame.
Related
I want to show a UIPickerView on top of a table of rows I've created. However, when I place the UIPickerView on my Storyboard and show/hide it, it leaves a blank space where the Picker was. How can I remove the picker completely and make the rest of the rows fill the space?
EDIT: I finally fixed the issue. It was extremely difficult to find the correct info. A lot of the answers are saying to set a height constraint outlet, which I tried to do but there was no way to get the height outlet from IB into my code. I even tried putting it into a stack view and couldn't get the outlet to work either. However I was able to get the frame size and set the frame size like so:
// hide
StoryTypePickerView.isHidden = true
let size = StoryTypePickerView.frame.size
StoryTypePickerView.frame.size = CGSize(width: size.width, height: 0)
// show
StoryTypePickerView.isHidden = false
let size = StoryTypePickerView.frame.size
StoryTypePickerView.frame.size = CGSize(width: size.width, height: 100)
If there's a better way, I would be happy to hear it. This works for me for the time being.
Take height outlet of Picker view .when you want to show the Pickerview that time set height of pickerview(Ex. self.height.constant = 160) and when you want to hide picker that time set height of pickerview as 0.
Create view includes date picker above the tableview and give height constraint to container view. When your hiding the view make the hight contrints value to 0 and vice versa... Hope it will work!.
I finally fixed the issue. It was extremely difficult to find the correct info. A lot of the answers are saying to set a height constraint outlet, which I tried to do but there was no way to get the height outlet from IB into my code. I even tried putting it into a stack view and couldn't get the outlet to work either. However I was able to get the frame size and set the frame size like so:
// hide
StoryTypePickerView.isHidden = true
let size = StoryTypePickerView.frame.size
StoryTypePickerView.frame.size = CGSize(width: size.width, height: 0)
// show
StoryTypePickerView.isHidden = false
let size = StoryTypePickerView.frame.size
StoryTypePickerView.frame.size = CGSize(width: size.width, height: 100)
If there's a better way, I would be happy to hear it. This works for me for the time being.
On my storyboard I have 2 image views.
One holds the main image, the second is an overlay which will be used to specify the crop area of the main image.
In my viewDidLoad I've done this.
let screen_width = UIScreen.mainScreen().bounds.width
let screen_height = UIScreen.mainScreen().bounds.height
self.overlayImage.frame = CGRect(x: self.imageView.frame.origin.x, y: self.imageView.frame.origin.y, width: screen_width, height: (screen_height * 0.1919))
The goal is to have the overlayImage's top left corner lined up properly with the imageView's top left corner. Also the height of the overlay should be about 1/5th of the screens size. However when I run the code the overlayImage is exactly the same size and in the same location it was originally on the storyboard.
How can I programmatically line it up on top of the imageView after the image has been set to it, and also resize the overlayImage dynamically in the viewDidload?
I'd just do it manually in storyboard editor but everyone will have different screen sizes so I thought it best to use the mainscreen().bounds.height variable to determine the amount of height to use dynamically at runtime.
All you have to do is to write your above code in viewDidAppear instead of viewDidLoad as below:-
override func viewDidAppear(animated: Bool) {
let screen_width = UIScreen.mainScreen().bounds.size.width
let screen_height = UIScreen.mainScreen().bounds.size.height
self.overlayImage.frame = CGRect(x: 0, y: 0, width: screen_width, height:screen_height/5)
}
And your frame will set itself as desired.
Since you're working with Storyboard, I would rather use Autolayout and Constraints to solve this problem for me... For instance you set the constraints for your first image view, then set the constraints of your overlay based and related to the former one. Example:
Lower UIImageView: set leading, trailing, top and bottom constraints
Overlay: set center X and Y constraints to be equal to the lower Image
Overlay: set height and width to be proportional to the lower image
Thus, when ever the screen size changes, the two images will always resize equally...
Interface builder:
Set overlay top, leading, width to your image view.
give your overlay a constant height constraint; the constraint cosntant value is arbitrary because you will replace it at run time
ctrl + drag an IBOutlet from this height constraint to your viewcontroller
in your viewWillLayoutSubViews set the .constant of your height constraint to UIScreen.main.bounds.size.height / 5
I feel like this is a softball for you veterans, but I'm having a lot of trouble resizing the subview of my UIImageView... here is what I got:
var myImageView:UIImageView!
var tmpView:UIImageView!
myImageView is my "main" UIImageView, and I'm treating tmpView as a subview... now I've tried both with and without auto layout on, but I've set the constraints of myImageView s.t. myImageView takes up the whole screen. I've confirmed this to be true by setting myImageView.image = UIImage...etc.
Here is my code for initializing the image and adding it to the subview:
self.tmpView.image = UIImage(named: "myImage.png")
self.tmpView.frame = CGRect(x: 0, y: 0, width: 200, height: 300)
self.imageView2.addSubview(self.tmpView)
Now here is where I am running into problems - no matter what I set tmpView's height and width to, the size never changes. Interestingly though, changing x,y does have an effect on the position of the subview.
Here are my questions: 1) Why do both x and y obey nicely, but width and height do not?
2) How do I fix this, and is it best to do so programmatically or through the storyboard?
You should no longer modify the frames directly when using autolayout. Add a width and height constraint to tmpView, and create outlets for them if you're using IB (The outlets should have the type NSLayoutContraint).
You can also create the constraints in code but there's no point of doing it that way unless you have to.
In both cases, to resize tmpView, change the constant property of each of the constraints to the values you want.
widthConstraint.constant = yourNewWidth;
heightConstraint.constant = yourNewHeight;
Looking to fill the background image in Xcode iOS based on a percentage.
The Constraints
The image must fill the entire screen (centered), so that the smallest dimension (width or height) fits. (Even if image becomes pixelated because it has a small file size.)
This must work universal, both iPad and iPhone devices.
Examples
You can use UIImageView. To make an image fill the screen, set imageView.contentMode to ScaleAspectFill.
To make UIImageView fill the screen, use auto layout. The easiest way is to add 4 constraints for UIImageView. Spacing to nearest neighbor = 0 will do. Like so:
Or leading/trailing/top/bottom space to superview = 0, if you like.
Set height and width of your image view to the width and height of your screen
var image:UIImageView = UIImageView(frame: CGRectMake(0, 0, self.view.frame.width, self.view.frame.height))
You want to make a UIImageView with the contentMode set to .ScaleAspectFill and add it as the bottom subview of your view.
How can I vertically center the content that I have inside a accessoryView from a UITableViewCell? The content is represented by a dynamically added UISwitch that is resized to 50% both width and height using:
switchView.transform = CGAffineTransformMakeScale(0.5, 0.5);
A picture with the problem is:
You cannot change the position of a accessoryView. Please refer to the following question:
Can a standard accessory view be in a different position within a UITableViewCell?
It is okay for you to apply transformation such as scale, and rotation to the accessory view. However, you are not able to apply translate transformation to the accessory view. For example,
switchView.transform = CGAffineTransformTranslate(CGAffineTransformMakeScale(0.5, 0.5),10,10);
Only the scale part of the transformation is applied to the switchView. Looking through the apple documentation, I also found this following line of note:
... The accessory view appears in the right side of the cell.
I don't think apple do want you to customized the location of the accessory view. Based on your question, I also tried changing the centre and the frame of the accessoryView, I do notice that the position of the accessoryView is not moving at all.
In short, If you really want to change the size of the UISwitch and move it to the vertical align it in the table view cell, I think you have only one option: You will have to add UISwitch as a subview of the cell and then you will have to freedom to do whatever you want.
Set the appropriate constraint in interface builder: "Vertical Center in Container".
I would not recommend reducing the size of standard UI elements.
The best way I could do this is by subclassing UITableViewCell, and overriding layoutSubviews and positioning the accessoryView there:
- (void)layoutSubviews {
[super layoutSubviews];
self.accessoryView.frame = CGRectMake(self.accessoryView.frame.origin.x + (self.accessoryView.frame.size.width / 2), (self.frame.size.height - (self.accessoryView.frame.size.height / 2)) / 2, self.accessoryView.frame.size.width, f.size.height);
}
I would probably advise you not to resize Apple's prebuilt UI components, especially because Apple's Human Interface Guidelines suggest:
Give tappable controls a hit target of about 44 x 44 points.
The resized switches are smaller.
Nonetheless, I hope this helps!
iOS 15.5 / Swift 13.4.1
It's a little bit weird when I set a UIImageView to cell.accessoryView
let iconView = UIImageView(image: .sfSymbol(of: "lock.fill", size: 17, weight: .semibold))
iconView.bounds = .init(x: 0, y: 0, width: 32, height: 32)
iconView.contentMode = .scaleAspectFit
iconView.debugBorder() // Show 1pt border
self.accessoryView = iconView
But when I use an UIView as image view's container, add top/bottom/leading/trailing auto layout, then set container to cell.accessoryView, the image is vertically centered in cell:
let imageContainerView = UIView(frame: .init(x: 0, y: 0, width: 32, height: 32))
let imageView = UIImageView(image: .sfSymbol(of: "lock.fill", size: 17, weight: .semibold))
imageView.contentMode = .scaleAspectFit
imageContainerView.addSubview(imageView)
imageView.debugBorder() // Show 1pt border
imageView.labr.box(imageContainerView, padding: 0).done() // Auto Layout: fill container
self.accessoryView = imageContainerView