Show and hide UIPickerView in a TableView without moving the rows - ios

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.

Related

How can I change the position of UILabel within a cell of a tableview

I would like to move the position of a UIlabel within a cell of a UICollectionView so that the Y value of the UILabel is 100 higher.
It's position is already set in the storyboard but I would like the position to change under a set of conditions such as where the UIImage in the cell isn't needed.
I have tried various options including below without succes
cell.flashLabel.frame = CGRect(x: 5, y: 70, width: 230, height: 60)
I'm not able to check this at the moment but have you tried
cell.setsNeedsLayout or cell.setsNeedsDisplay?
This should update the cells views to the new frame. Call one or the other after setting your frame.
Let me know if that works. If not, I can take a look when I get out of work.
- Amir
You could try linking the top constraint of the label to an outlet. You would then be able to manipulate the constraint using:
cell.topConstraint.constant = 70 //whatever point value is needed

Swift - setting UIView's size based on size screen

I went through many threads here and tried two most recommended solutions.
Inside ViewDidLoad() method:
self.darkBackgroundWithButtons.frame = CGRectMake(0, 0, UIScreen.mainScreen().bounds.width, UIScreen.mainScreen().bounds.height * 0.254)
or
self.darkBackgroundWithButtons.frame = CGRectMake(0, 0, self.view.frame.width, self.view.frame.height * 0.254)
Also, in my storyboard I set low priority of the view's height constraint
(If I don't set height in storyboard, xcode would complain about ambiguous layout)
But none of these lines of code above does anything to darkBackgroundWithButtons, it remains the same height for each device size
This probably is the problem:
In interface builder you set constrains to your button, and therefore it doesn't change its height when you try to update the frame. Do this instead:
First connect your constrain from interface builder to your viewcontroller, just how you would normally do it with a button.
Then use this code to change the constrain:
var index = self.darkBackgroundWihButtons.constraints.indexOf("your constrain name")!
self.darkBackgroundWithButtons.constraints[index].constant = 0.2 // or whatever number you want

Adding subview to scrollview

I'm trying to add a subview to a scrollview I have in my view controller:
let size:CGSize = self.view.bounds.size;
self.scrollview.contentSize.width = size.width
pageMenu = CAPSPageMenu(viewControllers: controllerArray, frame: CGRectMake(0.0, self.tableView.frame.origin.y + 130, size.width, size.height), pageMenuOptions: parameters)
self.scrollview.addSubview(pageMenu!.view)
It works to the extent that it adds it in the correct position and height I want it. But for some reason, right now it only expands to about 60% the width of the screen (I need it to be full screen).
Things I've tried
1) Setting it so self.view.frame.width
2) Setting it to the width of another full screen element.
3) Setting it to UIScreen.mainScreen().bounds
I checked the constraints of the scrollview in storyboard and it's configured to be full screen...so I'm not sure why this wont work.
This issue is related to the constraints that need to be set to the scroll view. I have answered a similar question here. Basically you need to specify a constraint for the scroll view's content view's width. See my answer in above link for a detailed description. The problem is that the scrollview adjusts its size to its content view's size even after we provide proper constraints to the scroll view. So we need to specify the constraints of the content view of the scroll view with respect to the scrollview and its superview so that the content in the scrollview fits our requirement.
One thing I noticed was that in your CGRectMake code you are specifying your y origin to be tableViews y value + 130. That seems like the problem to me.

Shrinking icon image in tableView cell

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.

swift - I'm dumb and my subview wont obey and correctly size itself

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;

Resources