viewDidLoad or didLayoutSubviews for setting rounded corners? - swift

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.

Related

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:

Correct way to autosize all view objects

What's the correct way to autosize or automove all view's components when call status bar appear (status bar height change)?
I'm using:
- (void) application:(UIApplication*)application
didChangeStatusBarFrame:(CGRect)oldStatusBarFrame { }
but in this way i need to set manually the frame of all the elements. I think that not is the best way.
I tried to set autosize in interface builder panel, but nothing change when status bar change frame.
Is there a correct way to do this in all of my view without set frame to every elements in every view?
thanks.
Using - (void) application:(UIApplication*)application didChangeStatusBarFrame:(CGRect)oldStatusBarFrame is not a good place to do this.
The solution will depend on your desired layout. If it is simple enough the autosizing mask will work fine. For more complex layout you might want to subclass UIView and use it as a container for the subviews. You can then override layoutSubviews to perform the correct layout depending on the bounds of the view (with the container being autoresized).
For autoresizing: e.g. If you want to have all labels to be fixed with respect to the bottom border and have a fixed height: simply click the bottom spacing in the autoresizing dialogue and remove the height arrow.

UIView: Rounded Corners Without Performance Issues

I am using an AQGridView to display my data in a grid on iPad. Every cell is a UIView subclass and typically, there are 18 cells displayed simultaneously.
I would like to add a round corner to these cells, so I set the cornerRadius property of the relevant layers (i.e. the layer of the main UIView and of one subview). However, this results in performance issues and the scrolling is not smooth any more. When using other CALayer properties, such as shadowOpacity, this does not happen.
Are there any other ways to add a rounded corner (apart from using an image)? Or am I doing something wrong?
I also saw a major performance hit when using cornerRadius on the layer of a view that contained a UIImageView subview. Rasterization solved that problem: view.layer.shouldRasterize = YES;
It could be where you're placing the setCornerRadius call. Make sure it's somewhere that only gets called once, not, for example, in a drawRect method.

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.

Creating a UIView in Interface Builder that automatically centers itself when added as a subview

I created a UIView xib in Interface Builder and tried everything I could to indicate that the UIView should center itself, anchor itself at center, orient itself in central coordinates, etc. etc.
But whenever I add it as a subview in code, I also have to programmatically set its frame up with CGRectMake() or else it will always add to the top-left of its parent. The math to reframe it is pointless and ugly, so I presume I'm just not twiddling a bit in the IB inspector correctly.
Can anyone confirm this is possible, and if so, what I need to do in IB to accomplish this?
Why don't you just set .center of the subview just added, to be the point created by halving the width and height of the superview?
Either that or define the rectangle that view is going into with IB (I'm imagining a container view) and simply set the frame of the view you are adding to containerView.bounds (bounds is a position independent value and so x,y will be 0 while size will equal the container size.
Centering but maintaining size isn't possible in IB. Centering but maintaining margins to its superview is though.
You will have to override the layoutSubviews message or simply keep the calculation code you wrote.