I use UISearchBar inside of UITableView. And I need to set custom left and right margins of UITextField.
Marked with red line on the picture.
Xamarin or Swift example could be applied.
-
First, what do you mean by "UISearchBar inside of UITableView"? Usually it's separate search bar on view and then separate tableview below. Second, are you familiar with iOS autolayout? The easiest way to add margins in your case will be by using constraints. – Olter Dec 27 '18 at 10:37
-
@Olter I added TableViewController and added SearchBar in header section. – Atlantis Dec 27 '18 at 10:39
2 Answers
Usually, when you want to add a searchbar to table you do it this way:
Choose a UIViewController instead of UITableViewController. Add a UISearchBar on top. Add UITableView below. Set search bar to fixed height via constraints in Interface Builder. Set the search bar's left & right constraints you want. (In your situation it would probably be something like 20px to right)
However it's also possible to use search bar the way, you've implemented it.
If you've used section header for a search bar, then you can add those constraints programmatically. Set them in willDisplayHeaderView
method. That's a more complicated approach though.
Personally, I would rather go with first approach since it's much easier to set constraints in IB rather than programmatically.

- 1,129
- 1
- 21
- 40
If you want to add padding around the text inside the textfield this is how.
The image however shows the red line outside the the textfield, I would nest the UITextField inside a view and add the inset to that view.

- 3,419
- 2
- 25
- 49
-
No, I need to increase left and right margins of UITextField. I have to set space which marked with red line equals 20pt. – Atlantis Dec 27 '18 at 10:53
-
Yeah so create a new view that will contain the UITextField, add the spacing there, then add that as header instead of the UITextField, like the second part of my answer states – Casper Zandbergen Dec 27 '18 at 10:56