Swift resize view based on shown Items with autolayout - xcode

so im toying around with Autolayout with a Storyboard.
So i have a View, containing a Button and to its right a TextField. (Sadly i cant post Pictures here yet) ;)
now what im trying to do, is that by default, when my view gets shown the button is hidden, and the view kinda resizes it self to only contain the Text field.
And after the text field is clicked (selected) the button is shown and with this the view resizes itself to fit the button and the Textfield.
I think i might be able to do that in code, by simply updating the View width with the width of the contained and shown items.
But i feel like there has to be a easy way to do that with Autolayout.
So i tryed many things, but it just doesnt work, no matter what constrains i add.
How can i solve this ? Thanks in Advance.
EDIT
i forgot to mention, even if i remove the button from the superview i get bad results, i suppose the reason is that the constrains get messed up if i remove the Button.
So thats not an Option.

Related

How do I place text fields correctly within an animated UIView?

I am having problems with animating an UIView.
Everything is actually pretty basic:
My storyboard, because it easier to show a pic than to explain everything.
As you can see, there is a white UIView in the middle ( behind the gray label that says "Navi ein" ). It has to TextFields and one button in the right bottom corner.
I now added an animation to this UIView, so when the user clicks the button "Navi" (placed in the navi.-bar, top left), the UIView flys in from the top to the center (centerY-constraint and alpha animation, very simple).
The animation works for the UIView, but the TextFields and the button are not shown after it. My question is: why? I would really appreciate your help! :-)
Please let me know if you need some code extracts, but it shoudn't be necessary.
EDIT: I now found the text fields and the button in my View Debugger, I just forgot to uncheck "Only show displayed views". But how to figure out what makes them disappear (or not even appear) when the app launches?
View Debugger
I am so sorry! I just saw that I set all three items to hidden in my code.
I've never thought about that so far, I just looked in my storyboard.
So the problem is solved, I just deleted the lines "streetTextField.isHidden = true" and so on.
Nevertheless, I think it is good to hear and to see that some problems that seem to be hard are very easy to solve.

NSToolbarItem and Auto Layout

I have a window with a programmatically created toolbar, which is populated with an NSToolbarItem that has a custom view defined in a xib file. If I check "Translate Masks into Contraints" for the view, then it doesn't resize itself correctly, so its content gets squashed even though I set its compression resistance priority to 750. If I uncheck that on the other hand, then I get a "Detected missing constraints" error at runtime. Also, the content of my view then resizes itself correctly, but it ends up clipped like so:
So it looks like the toolbar sticks my view into a container view and, in order to get the correct behaviour, I should set up layout constraints that allow my view to position itself correctly. However, I don't see how to access that container view... Any idea what I am doing wrong?
I am probably missing something obvious since one would think that this is a very standard setup, but Google didn't come up with anything.
Answering my own question, I opened a support request with Apple, the outcome of which is that this is now being treated as a bug by Apple.
In case anyone else runs into this, a good workaround is to create an intermediate view which contains the toolbar view as a subview with constraints #"H:|[toolbarView]" and #"V:|[toolbarView]", has translatesAutoresizingMaskIntoConstraints=YES and to use this view as the toolbar item. It then suffices to listen for size change notifications of toolbarView and to adjust the size of its superview accordingly.

Center popover on the previous view

I want to implement popovers in my app. When I change the storyboard segue option "kind" too "popover" I get an option to create a popover that has an anchor point with an arrow pointing to whatever I anchor the view/popover to. I don't want this. I want a popover that is centered on the previous view so you can still see parts of the previous view.
Picture of what i mean here:
I want the popover to behave like an independent view controller with buttons/labels etc. but obviously be smaller so you can see whats behind it. I also would like to know how to get the faded/darkened effect of the previous view after the popover shows.
You can Achieve this by using popover.swift third party library available in github.There is no issue from my end

Can't get the most basic scrollview to work

This is not my day. I've just deleted every object from my previously completed view controller to start over because I can't get a simple scroll view to scroll. I've got a view controller that is embedded in a navigation controller. I put scroll view inside the view controller and then put two text fields inside the scroll view, one field at the top and one at the bottom.
First of all, when I run this, it won't scroll. What is the point of the scroll view if it won't scroll?
Second, I can't figure out how the relationship between the navigation bar and the scroll view works. Is the scroll view behind the navigation bar? Is it below the navigation bar? In the story board, it looks like it is behind the bar, but when I run it, my text field is pushed down so far that it looks like it is behind it.
Here are a couple of screen shots. To say that I am frustrated is an understatement. Thanks for any help getting me out of my circular problem. I appreciate it.
And here is how it looks when I run it. Notice how far down the top text field gets drawn. Also note that you cannot see the bottom text field. I am unable to scroll to this. What gives?
First of all, when I run this, it won't scroll. What is the point of the scroll view if it won't scroll?
Scroll views won't scroll by default unless their contentSize exceeds their bounds. You can force it to scroll anyway by setting alwaysBounceVertical to YES.
Second, I can't figure out how the relationship between the navigation bar and the scroll view works. Is the scroll view behind the navigation bar? Is it below the navigation bar? In the story board, it looks like it is behind the bar, but when I run it, my text field is pushed down so far that it looks like it is behind it.
Yes, in iOS 7, by default, a scroll view will start at y = 0, behind the nav bar. You use the contentInsets property to push it down. The view controller has an automaticallyAdjustsScrollViewInsets property that can handle this for you.

Padding around an NSTextView in an NSScrollView

I'd like to provide a few pixels of padding around an NSTextView inside of an NSScrollView. I've reviewed this post and using setTextContainerInset does the trick for the left, right, and bottom margins. But as soon as the text scrolls the top buffer is lost. Same thing with overriding textContainerOrigin. Another answer in that post states:
The way TextEdit does it (when in Wrap to Page mode) is to put the text view inside of a larger view, and set that larger view as the document view of the scroll view.
But if I do that (using, say an NSBox) the content no longer scrolls. Am I missing something regarding that particular trick, or are there any other techniques that folks could suggest?
There's a step that seems to be missing from your quote. You'll need to make sure your new document view tracks the changes in the text view's frame and sizes itself to fit. You can turn on NSViewFrameDidChangeNotification on your text view with -setPostsFrameChangedNotifications:, then have your new document view listen for notifications from your text view..
By the way, I ended up accomplishing this by subclassing NSClipView, overriding setFrame:, setFrameOrigin:, and setFrameSize, and hacking the origin and width in those methods to add my padding.

Resources