How to change searchBar width programmatically? (Swift) - 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

Related

Make UITableView ignore Safe Area

I have a TableView inside a ViewController.
I made the TableView stretch to the View SuperMargings (with constraints) and disabled all SafeArea Inset options but my TableView is still under the SafeArea when I run my project.
How can I make my TableView go full height on iPhones with notch?
If you have already pinned tableView to it's superview(not to safeArea) with constraints but tableView still respects safeArea there is property contentInsetAdjustmentBehavior in UIScrollView(UITableView is subclass of UIScrollView as we know) since iOS 11.
This property is UIScrollView.ContentInsetAdjustmentBehavior enum with 4 options.
You need to set .never to contentInsetAdjustmentBehavior.
What works with any view is to associate the bottom constraint of the table view to the bottom anchor of the root (or parent) view (instead of the SafeArea).
This can be done in the storyboard editor double clicking on the constraint in the right inspector and changing the first item anchor from SafeArea to Superview (or any wanted view).
You can then set the edge insets at will if needed (to avoid content remain partially hidden behind the rounded frame corners or the notch if applying the same procedure to the top anchor)

Setting Buttons in Head of view

I am working with an app for macOS. So I want to set six Buttons in top of view using the complete width of view. When I resize the view the buttons should resize at the same time, so the width of the buttons should raise as the view is raising in same relation
How can I do that. Is it possible using the autolayout? Or do I need to change the position (x, y) and size programmatically? But how can I change the position?
Thank you for your help
Best regards
Programmatically, position and the size can be set by adjusting the .frame property of the NSView object.
https://developer.apple.com/documentation/appkit/nsview/1483713-frame
Also, it could be archived with Auto Layout by setting the left and right constrains of each element (and not specifying a width constrain) so the distance between each element, and the distance from the left-most element to the view.minX and right-most element to view.maxX are fixed (while the width is calculated dynamically). Additionally, add a height constrain so the heights remain the same.
Please follow up if you need more information ;)

MKMapView maximized, but I do not want it

I got an issue and I don't know why I have it and how to fix it! Maybe it is only a simple problem. Here are two screens:
There is something missing! Where is the Button? And why is the Map at full screen? It seems a bit strange can anyone help?
This is undoubtedly a function the screen going from a 4" screen when previewing your screen in Interface Builder to a 3.5" inch simulator/device screen.
If using autolayout (open up the storyboard, click on the first tab, the "File Inspector" and see if "Use Autolayout" is checked or not). Check your constraints. The map view should not have a height constraint (or if IB won't let you get rid of it, just drop its priority down to the lowest possible value...though if it adds the height constraint back in, it means that your other constraints are not fully qualified, such as neglecting the button's bottom constraint) but should have either a bottom constraint to the bottom of the screen or, better, to the button. Also make sure that the button's top constraint goes to the map view and that it has a bottom constraint that goes to the super view. Interface Builder makes this process far more complicated than it needs to be in its effort to add what it determines as missing constraints, IMHO, but it can work. It took me 2-3 minutes of fussing with all of the vertical constraints to make sure that they were both unambiguous as well as satisfiable at both screen heights.
If not using autolayout, check your autosizing masks (located on the "Size inspector" tab). You want flexible height and fixed bottom.
In IB you can toggle the appearance of your view from 4.0" to 3.5" by pressing the button. Just adjust your autolayout constraints or non-autolayout autosizing masks and then toggle back and forth between 3.5" screen and 4.0" screen to confirm whether everything is configured properly.

how do i work on a taller view in interface builder?

OK, so I am learning to use UIScrollView in interface builder. since the scrollView itself does not have any content, I created another view, the contentView, to hold my controls and scrollable content. into this view I place controls, labels, etc and then in my code i set the contentSize of the scrollView to the size of this contentView.
My question seems stunningly simple and so obvious that I must have missed something somewhere. when I created this XIB in IB I got your standard empty iPhone interface window. I dropped a scrollView on top of it, it took up the whole window. I dropped a view on top of that, it took up the whole scrollview. I added some controls, which so far I can still see inside the contentView rectangle in IB.
My question is how do I work on/add controls which lie outside of the visible part of the contentView in IB? LOL. it seems so simple, but i just don't get it. I can set the height of the content view or drag the rectangle to whatever I like (and indeed this is the whole point of having a scrollview) but the fixed UI window from IB won't expand so i can see the "offscreen" part of the contentView to add more controls. It's like it's just fixed at that size because that's the size of one iPhone screen and it won't let me make it any bigger/taller.
what did I do wrong?
-a
You need to turn off all simulated user interface elements (like the status bar) to be "undefined" except for size, which you select to be "Freeform" from the drop-down, and then you can set the view height using the Ruler tab to be whatever you like.
For example, here I've selected a photo view controller, and set the size in the right side bar to be "iPad Full Screen" - but I could also change that to "freeform" to set any height I wished.
First add a ViewController by any which way you prefer. Then in interface builder, click on the view controller Icon (on left). In the connections inspector click on the ruler icon "Show the size inspector". There will be option list for simulated size, change to "Freeform" and increase the height to any size you want. Hope this helps.
neeever mind. you drag the content view up so that some controls are offscreen and then add more controls/expand to the part you just made visible by moving the top stuff off the top. in effect, you physically scroll the contentView in IB by dragging it with the mouse. seems a smidge counterintuitive, but whatever.
Set simulated size to freeform for the the view controller to a large enough size that you can add in your controls
You don't have to create a view to place inside the scrollview if you don't want. In your case, it sounds like it doesn't make much sense.
As for the second part of your question, you can place items directly on the scrollview (it is a view afterall), as subviews. If you want them to be off screen, then just set their frame up to be at those particular x, y coordinates you want it to be at. You will have to ensure your scrollview's contentSize property is large enough though to house your entire content, this is what allows scrolling horizontally/vertically.
You probably want to do the offscreen elements programmaticly instead of using xibs.

How do I set the right margin of a UITextView?

I have a vertically-scrolling UITextView that fills the width of the screen. I need the text view to have margins (contentInset) of 20 pixels on the left and the right. But for some reason, I can't get the right-hand margin to work, either in Interface Builder or in XCode.
The reason I can't just make the text view narrower is because I am adding a subview which needs to run the full width of the screen. Also, I can't turn subview clipping off, because it plays havoc with a load of other elements on the screen.
Anyone know why the contentInset property is not affecting the right margin?
Thanks!
You could add an intermediate view that can be the superview to your text view and what was previously it's subview.
New View with Frame(0,0,width,height)
->TextView with Frame(20,0,width-20,height)
->Subview with Frame(0,0,width,height)