I create a view called boundView using IB and Auto Layout,then in the controller I call [self.boundView layoutIfNeeded],then I pass self.boundView.frame.size to a method to generate the size of boundView's subview CardView. And then use the
PlayingCardView *playingCardView = [[PlayingCardView alloc]initWithFrame:frame];
to create subview programmatically. I do use NSLog to check that the size of subview is smaller than superview. But when I use [self.boundView addSubview:CardView] to add the subview. It is larger than superview!
Is there something wrong with the coordinate?Or it is because I combine the Auto Layout with the view I create by code?
Where are you doing this? If it is in viewDidLoad then the autolayout sizes will not have been calculated yet. Try doing it in viewDidLayoutSubviews.
Related
I have got a view and some elements in it
let background_img_view: UIView = {
let view = UIView()
return view
}()
background_img_view.addSubview(background_image)
background_img_view.addSubview(border)
background_img_view.addSubview(about_txt)
about_txt size is unknown, it can be 30px or 300px, now I want my background_img_view's height to depend on about_txt's height.
How can I make it happen programmatically?
Have you tried using the sizeThatFits(_:) method for the about_txt view. Use the size that this function returns and set the superview's size to that.
https://developer.apple.com/reference/uikit/uiview/1622625-sizethatfits
If you have the subviews added before the view is on the screen, you could set the height in the viewWillAppear method of your view controller. You can also update the size in viewWillLayoutSubviews.
If you're using auto layouts, have a look at setNeedsLayout vs. setNeedsUpdateConstraints and layoutIfNeeded vs updateConstraintsIfNeeded
I'm using Storyboard with autolayout. In my ViewController I have a basic structure: one UIScrollView and one contentView (UIView) that contains different labels). Then I have different elements that I add in the viewDidAppear method of the class inside my contentView , then in order to recalculate it's frame size I do this:
- (void)fixContentViewHeight
{
_contentView.frame = CGRectMake(_contentView.frame.origin.x, _contentView.frame.origin.y, _contentView.frame.size.width, operators.frame.origin.y + operators.frame.size.height);
//constraint for contentView height
_contentViewHeight.constant = operators.frame.origin.y + operators.frame.size.height;
}
- (void)fixScrollViewHeight
{
_scrollView.frame = _contentView.frame;
_scrollView.contentSize = _contentView.frame.size;
}
where operators is the LAST placed element of contentView, it's always on the bottom of the frame. I call these 2 methods inside the viewDidAppear and they make the view scrollable, the problem is that the frame of contentView doesn't get updated so the last elements are always unclickable (because they're placed outside the frame of the view).
What am I doing wrong? Why the scrollView becomes scrollable but the contentView keeps it's old frame?
If you have autolayout constraints affecting the height of _contentView You will not be able to change its height by setting the frame, as the constraints will override that.
You will need to (and should be) adding new / modifying constraints in your code when you are adding new elements. then calling
_contentView.layoutIfNeeded()
After all the updates to let the UI update if it needs to, which it should.
Please update UI On Main Queue
dispatch_async(dispatch_get_main_queue(), ^{
// update any UI on Main queue
});
I think that all your frame related information should be inside viewWillLayoutSubviews() and viewDidLayoutSubviews(). You can add stuff in viewDidLoad() but for any frame management I would use mentioned methods.
In your particular situation use former, viewDidLayoutSubviews(). However, you can resolve these kind of issues using constraints connected to the code as #Simon McLoughlin mentioned. 'scrollView becomes unscrollable' means that you should update contentSize as well so keep an eye on that as well.
Try to add vertical space constraint between scroll view and your last placed element.
I have an UIView inside which i have a UIButton. I am using auto layout. Now I want to reduce the height of the UIView. How should I do it ? On reducing the height of the UIView , the UIButton inside the view should also move up at the same time. How can i do this using Auto Layout ?
You can create IBOutlet for height constraint of you view and you can change value for it.Just check how to create.
Now use the code below to set height of you UIVIew
heightConstraintOfView.constant = DYNAMIC_HEIGHT;
I have a view that is larger than screen, I need to put it in a UIScrollView.
So I first add an UIViewController to story board, then, I add a UIScrollView to the root view of my view controller, then when I add subviews of UIScrollView, but I can't add them outside the scrollview area I can see, how to solve this problem?
Change the simulated size of the view controller to Freeform and then you can set the size to whatever you want. After editing, you may want to set it back.
Edit 1:
1) put your subview into scrollview's visible area
2) change frame of the new subview as you wish
3) change contentSize property of scrollview with runtime attributes
Edit 2:
u can create a new view with xib, which contains all your subviews, and then add this view on scrollview OR u can use storyboard's Container View like this:
p.s.: don't forget about contentSize (point 3 in my first edit), but if you are using auto-layout, you need to set it programmatically like this:
- (void)viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
_scrollView.contentSize = CGSizeMake(320, 250);
}
put a scroll view in the view controller ,and put a view inside the scroll view named containerView.
design the big content in another view controller (named bigContentView)
in code ,view load event ,
a. get the bigContentView by storyboard Id. ( get the controller and use controller.view)
b. create the outlet to the containerView.
c. update the containerView's frame according your requirement.(update the width and height)
d. bigContentView.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
e. containerView addSubView:bigContentView.
I have a view hierarchy under a UIViewController that consists of a main view and a few subviews of the main view, all defined in a storyboard, and all connected using IBOutlet.
I would like to set the position and size of the subviews relative to the main view.
I've tried setting them in viewDidLoad and viewWillAppear. Neither work, and when called, the main view has valid frame values but the subviews all have frames of (0,0,0,0).
If I do nothing, the subviews appear exactly where positioned on the storyboard. I need to make the following code work, but I don't know where to put it.
CGRect newFrame = CGRectMake(10,10,50,50);
[subView setFrame:newFrame];
Another way to ask this question:
Where and when do the frames of storyboard subviews get set?
Or, should I just create all the subviews with code?
when using autolayout you can set frame of the views (if the frame is calculated from super view's frame) in viewDidLayoutSubviews method or the other way I use is calling this method [view setTranslatesAutoresizingMaskIntoConstraints:YES]; and then [view setFrame:...];. Hope this helps.
The position and size of subviews can be set when the UIViewController calls viewDidAppear but that causes the presentation to jump. Setting the position and size of a UITableView in viewDid/WillLayoutSubviews does not work.