iOS 7 UIDatePicker height inconsistency? - iphone

I'm running into something weird when using UIDatePicker elements with Storyboards in iOS 7. In the Storyboard, the date picker has a fixed height of 162. In reality, however, the element takes up more space than that. So this
turns into this:
so I have to move everything below it down, guessing and eyeballing how much space the date picker will actually use. Is this a bug? Am I doing something wrong? To be clear, the date picker is totally transparent - can't figure out a way around that. The white background at the top is the main UIView, and the gray background is the background of the UITableView embedded inside the container view.

I can confirm that using UI(Date)Picker in storyboards has a different height (162.0) than in "reality" (216.0). Therefore you have to adjust the space to container view to fit that "real" date picker height or try to solve it using auto-layout.

Here is a funny trick I just found: put the UIDatePicker inside a dedicated view with a constraint of 162 points in height (add 0 point vertical constraints from top and bottom of the picker to this new superview). This seems to have the effect of forcing the picker to keep the size of 162 points.
You can add the option of clipping the subviews to be sure that the UIDatePicker will not escape from its new prison.
EDIT: after some more tests, it seems that by just adding a height constraint of 162 points to the UIDatePicker, it will keep its "IB size". And, to answer #Andrew's comment, here is what it will look like:

You can change the width and height by simply giving it width and height constraints. Without doing that, the UIDatePicker just acts weird, I've found.

Setting clipsToBounds property to YES on my UIDatePicker object helped me.
datePicker.clipsToBounds = YES;
For XIB's you can directly tick the checkbox for Clip Subviews :

Related

viewDidLoad or didLayoutSubviews for setting rounded corners?

i'm looking for the perfect place in my code to set rounded cornerRadius to my views.
Sometimes it seems like the UI shrinks my view after i set the cornerRadius to frame.height/2 so im getting a "more then round" view (looks like an eye)
I'm building my App with Interface Builder, so I thought viewDidLoad should be the right method to call myButton.layer.cornerRadius = myButton.frame.height/2
I also tried to design my views in viewDidLayoutSubviews but there were also some strange results
So my general question is:
Where should I place my cornerRadius code to be sure that I'm getting the right result?
Thanks for your replies!
EDIT:
The problem is especially with buttons (or views) with proportional height to superview. With fixed height its working fine!
Have you only placed the myButton.layer.cornerRadius = myButton.frame.height/2 in viewDidLoad?
If placed it in both views then remove it from subviews.
And make sure that the button is a "Square" in Size. I mean height and weight are equal.

StackedView inside ScrollView in Xcode 8 / Swift 3

I'm trying to arrange stacked views so I can set out the UI of my app to look neat and tidy.
The only problem I'm having at the minute is getting it set up so I can see it!
At the minute, it looks like this on Main.storyboard and it's annoying me:
Storyboard view
My constraints are currently set up as follows:
Constraints (edited)
Where am I going wrong?
Your UIScrollView needs to know its content's width. Since it can scroll both horizontally and vertically, it does not act like a UIView on interface builder.
One thing you can do is adding a hidden UIView in UIScrollView with 0 height and |-[WidthView]-| constraints (leading and trailing to its superview - UIScrollView). And then you should add WidthView.width = UIScrollView.superView.width constraint.
This makes sure that your UIScrollView's contentView's width is always equals yo UIScrollView's superView's width.
Your view hierarchy should look like this:

How to change searchBar width programmatically? (Swift)

I'd like to resize my app components when device is turned into left or right side - when it keep horizontal position
I'm checking device rotation in willRotateToInterfaceOrientation event and then I have to change width of searchBar and tableView. How should I do it? I tried some variants with components .frame and with CGRect, nothing works.
Update:
it can be done without any code. View my answer below
If you laid out your elements in Interface Builder then you can just add an outlet to the width constraint of your search bar. Then, change the constraint's constant property to change the bar's width.
I found the way to do I need
So, the task is that components will be resized in width when device turns horizontally.
1) Select items must be resized
2) Select "Leading Space to SuperView"
3) Without unselection, select "Add missing Constraints"
That's it. Now they will be resized correctly

Hiding elements outside of bounds on iPhone

I'm trying to get a UIView to expand (with animation), sort of like an accordion menu. I can get the animation working fine however the issue is the subviews of the UIView are expanding past the bounds of the UIView.
For example, the view has an UILabel in the top-right corner with a height of 16. Assume the UIView height is 0 at the beginning of the animation. One would expect that the content of the view be hidden and gradually reveal as the UIView grows. Once the height has reached 8, for example, half the label should be visible. However that isn't the case - rather, the label is visible the whole time, regardless if it's height extends outside of that of it's parent view.
Any ideas how to resolve this?
Okay, I had to set the clipsToBounds property to true. I spent some time googling before making the question but didn't have much luck until I saw it in the Related section of my question.

Resizing TableViewCell when TableView is in grouped mode

I have a TableView in my application and I've created a centered label inside TableViewCell that I'm placing in the view. When TableView is in standard mode everything is ok and the label is really centered. However when I change the mode to grouped then the label is displayed slightly to the right. I suspect this is because in grouped mode the table is a bit shorter then in standard mode and since I've centered it for standard view in Interface Builder it's just being moved to the right.
I'd like to know if there's a way to automatically have centered TableViewCell label adjust it's parameters so it appears centered in grouped TableView or do I need to make it a bit shorter and move it a bit to the left manually to get what I want?
Assuming that the table will be initialized with a centered label, it should respond properly to future changes in width when you set its autoresizingMask to UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleRightMargin. But as far as I know you can't change a table's style after creation, so I'm not sure if this approach will fix your problem.
I've found a solution. I'm not very happy with it but it works. I just resized TableViewCell and it's contents to 300px width instead of default 320 and it fits ok.