How do I set the right margin of a UITextView? - iphone

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)

Related

How can I make ScrollView full screen? I placed the necessary constraints, but it doesn't work

I'm trying to make a ScrollView so that its size adjusts to the size of the text. I placed all the necessary constraints
, but when the application is displayed on the simulator, the content view starts from the safe area and also ends up to the safe area. Although I have pinned scroll view to superview and content view to scroll view.
I want the content to start not from the safe area like here ,
but from the start of the screen like here
in other words, I want to remove the blue bar from the top and bottom
Do you have any ideas how to do it?
Sometimes UITableView & UIScrollView can still show safe area even though you attach your View to top of Super View. You just need to add one line to solve this issue but make sure your ScrollView is connected to superView not safe area at the top.
Add scrollView to your ViewController and in viewDidLoad() just write:
scrollView.ContentInsetAdjustmentBehavior = .never
Check this answer for further details: Make UITableView ignore Safe Area

Vertically centering UILabel programatically in scrollview that doesn't match the view swift

I have a horizontal scrollview that fills 1/3rd of the view.
I want to put a UILabel in the centre of the scroll view, which I have successfully done with:
let reagentsLocationx = mainscrollview.frame.width/2-100
However, I am struggling to centre the label vertically within the scrollview. When I use similar code for reagentsLocationy it centres the label in the view and not within the scroll view.
How do I centre the label, in Swift, just within the scrollview?
Many thanks.
I think this is because you are using frame and not bounds. I don't believe frame will ever be larger than your screen space, but bounds it equal to the view's actual size. Try basing it on the bounds of the scroll views height.
UIScrollView().contentSize.height might be what you are looking for.
No need of setting sizes in code.
Everything done in the storyboard.
Drag your scrollview into your view controller.
Set top, bottom, leading and trailing constraints from the scrollview to the parent view or safe area.
Drag your text label into the scrollview.
Set your top, bottom, leading and trailing constraints to the scrollview.
IMPORTANT set a second leading and trailing constraint, now from the text label to the parent view (UIView - not scrollview) or safe area.
This makes sure that the text label knows his width and so the scrollview knows it too.

Resizing a UISegmentedControl within a UIToolBar with constraints for different screen sizes

I have a UIToolBar underneath a navbar, and in this toolbar is a Bar Buttom Item which holds a UISegmentedControl. The problem is I can't add constraints to anything inside the toolbar. The UISegmentedControl pins itself to the left margin when I place it inside the toolbar/bar button item, but then I don't get any dashed blue line when I try to set it width to be the same from the margin on the right side. I see no options to center either, but I don't really want to center, I want to make the segmented control grow in width as the screen goes from iPhone4 to iPhone6+ (in portrait) so that its always some X-pts from the left and right margin. How can I go about doing this?
I have the width of the toolbar, which is 375 for a 4.7inch screen. I can also get the width of the segmented control for this size too, but I can't tell the correct width to make the margins on the left and right the same, I can only eyeball it.
You should add the UISegmentedControl as a direct subview to the UIToolbar (either via addSubview or directly in interface builder). While going the detour around UIBarButtonItem seems convenient here, it deprives you of the possibility to lay things out exactly as wanted, because UIToolbar will handle layout for you and you cannot override this.When you add the UISegmentedControl as a subview, you can add constraints as needed to make it fill the entire width.

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

custom keyboard UITextField inputView xcode

I am adding a custom keyboard to a view using inputView linked to a UITextField. I have set the size at Width 216, Height 320. I have it entering from the bottom on a portrait orientation. I want it to only cover 216 pixels of the screen but now when it comes up with the keyboard hugging the left side and has the correct aspect ratio. However there is a gray background that extends from the right edge of the keyboard to the right side of the view. Is there any way to remove this so it is clear and I can see the right side of the view? I have tried setting the background of the view to clear color and tried everything I can think of. Does anyone know if this is possible?
I guess you can't do it, because with keyboard opening the view moves up, so there are the "empty" rectangle in the 216x320 pts.