Here is my code:
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:items];
segmentedControl.frame = CGRectMake(0.0, 0.0, 320.0, 30.0);
The result is segmentedControl has about 10 pts margin left and top.
I try to change the params of its frame but cannot change x position.
So it always has 10 pt margin left. How to remove this margin?
Edit
I figure it out
UIToolbar has 10pt margin. If I add only UISegmentedControl, it will display correctly.
you can't change the height of UISegmentedControl because height is fixed for UISegmentedControl based on its style
for plain and borderd style the height is 43 and for bar style the height is 29.
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:items];
segmentedControl.frame = CGRectMake(0.0, 0.0, 320.0, 29.0);
segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
set UISegmentedControlStyleBar in segmentedControlStyle property of UISegmentedControl.
Use this
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:items];
segmentedControl.frame = CGRectMake(35, 200, 250, 50);
segmentedControl.segmentedControlStyle = UISegmentedControlStylePlain;
Use setter method
[segmentedControl setFrame:CGRectMake(x, y, width, height)];
Segmented control has the rounded rect border and that is why it displayse so.Jus set some color on its background and you can see the correct frame of the segment
Related
I'm creating a button and adding it as a UIBarButtonItem in the code below. I'm trying to make it a perfect circle.
UIButton *hasMessageBtn = [UIButton alloc];
hasMessageBtn.backgroundColor = [UIColor orangeColor];
hasMessageBtn.layer.frame = CGRectMake(0, 0, 24, 24);
hasMessageBtn.layer.cornerRadius = 12;
[hasMessageBtn addTarget:self action:#selector(openMessages:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *messages = [[UIBarButtonItem alloc] initWithCustomView:hasMessageBtn];
self.navigationItem.leftBarButtonItem = messages;
It looks like the size is wrong in two ways, though. The height never changes, and even thought the width changes when I change the width value, it seems to be incorrect because setting corner radius,
hasMessageBtn.layer.cornerRadius = 12;
doesn't completely close the rounded corners on the top and bottom. What am I missing here?
When you takes smaller the default size of custom view, its height will stretch the height to 34. That's why your leftBarButton getting distorted.
Solution:
Apply height and width constraint to barButton (Suggested *)
UIBarButtonItem *messages = [[UIBarButtonItem alloc] initWithCustomView:hasMessageBtn];
[NSLayoutConstraint activateConstraints:#[[messages.customView.widthAnchor constraintEqualToConstant:24.0], [messages.customView.widthAnchor constraintEqualToConstant:24.0]]];
Make your custom view's button size, CGSize(34, 34)
hasMessageBtn.layer.frame = CGRectMake(0, 0, 34, 34);
hasMessageBtn.layer.cornerRadius = hasMessageBtn.frame.size.height / 2;
As of iOS 11 you must set constraints to bar button items. Here is how it looks in objective-c:
[hasMessageBtn.widthAnchor constraintEqualToConstant:24].active = YES;
[hasMessageBtn.heightAnchor constraintEqualToConstant:24].active = YES;
And in swift:
hasMessageBtn.heightAnchor.constraint(equalToConstant: 24).isActive = true
hasMessageBtn.widthAnchor.constraint(equalToConstant: 24).isActive = true
If you have more inquiries about this topic I suggest you to look at 2017 WWDC video Updating Your App for iOS 11
In my UISegmentedControl I wish to show icons instead of text.
I cannot find any indication what the height of these icons should be. I would prefer to supply properly sized icons instead of having the OS resize them for me.
So, when calling
[[UISegmentedControl alloc] initWithItems:#[icon1, icon2]];
what should the sizes of these icons be?
You can give the segmentControl a bigger frame.
UISegmentedControl *test = [[UISegmentedControl alloc] initWithItems:[#icon1, #icon2]];
test.frame = CGRectMake(90, 90, 200, 80);
test.backgroundColor = [UIColor blueColor];
[self.view addSubview:test];
itemsArray should be the items images array.
I am trying to add segmented control to tableviewheader. I am using the following code to do that
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc]initWithItems:[NSArray arrayWithObjects:#"ALL", #"HOUSE", #"SENATE", nil]];
segmentedControl.frame = CGRectMake(24, 0, 272, 30);
[segmentedControl addTarget:self action:#selector(segmentedControlHasChangedValue:) forControlEvents:UIControlEventValueChanged];
segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
segmentedControl.tintColor = [UIColor grayColor];
[self.view addSubview:segmentedControl];
segmentedControl.selectedSegmentIndex = 0;
self.tableView.tableHeaderView = segmentedControl;
I can't set the frame, no matter what value is put its always 100% wide. How can i set the add 24px margin on left and right ?
That is because you are using the segment control as a tableHeaderView. a UITableViewHeaderView will be always be as wide as your tableView. You can only change the height.
Adding to akshaynhegde answer, you can easily achieve this by adding an extra UIView as parent and add the UISegmentControl to that UIView. In this case the parent UIView will take the whole width of UITableView but not the UISegmentControl.
So following should be the change in your code
UIView *view=[[UIView alloc] initWithFrame:CGRectMake(0,0,320.0,30.0)];
[view addSubview:segmentedControl];
self.tableView.tableHeaderView = view;
It should solve your problem.
Cheers.
First, you are adding segmentedControl on controller view [self.view addSubview:segmentedControl] and then assigning it to table header view
self.tableView.tableHeaderView = segmentedControl;
Which is wrong, you need to create a view with size table header size and then add segmentedControl add on view and assign this view to table header view.
Example
**UIView* view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 30)];
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc]initWithItems:[NSArray arrayWithObjects:#"ALL", #"HOUSE", #"SENATE", nil]];
segmentedControl.frame = CGRectMake(24, 0, 272, 30);
[segmentedControl addTarget:self action:#selector(segmentedControlHasChangedValue:) forControlEvents:UIControlEventValueChanged];
segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
segmentedControl.tintColor = [UIColor grayColor];
[view addSubview:segmentedControl];
self.tableView.tableHeaderView = view;**
I try to customize my UITextField object as it
but leftView is shifting right by some pixels as it
how can i change position of my UITextField.leftView
Here is my code
NSArray *itemArray = #[#"<", #">"];
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:itemArray];
segmentedControl.frame = CGRectMake(0, 0, 60, 26);
segmentedControl.segmentedControlStyle = UISegmentedControlStylePlain;
_urlTextField.leftView = segmentedControl;
_urlTextField.leftViewMode = UITextFieldViewModeAlways;
Try this ...
NSArray *itemArray = #[#"<", #">"];
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:itemArray];
**segmentedControl.center = CGPointMake(03, 15);**
segmentedControl.segmentedControlStyle = UISegmentedControlStylePlain;
self.textField.leftView = segmentedControl;
self.textField.leftViewMode = UITextFieldViewModeAlways;
Also change your textfield's border style to other than round rect..
Hope this helps
Regards,
Paggy123
UITextField doesn't provide a property like frame for leftView; it having only one property like bounds makes it like navigation controller bar button items positions; we cant change the leftView position . See here for reference.
I have this code in my viewForHeaderInSection: of tableViewController
UILabel *l = [[[UILabel alloc] initWithFrame:CGRectMake(20, 0, 320, 40)] autorelease];
l.backgroundColor = [UIColor clearColor];
//l.textAlignment = UITextAlignmentLeft; //i tried it didn't help!
l.text= NSLocalizedString(#"My Label:", #"");
the label always appears around 35 point off the cell's left side. If i use the UITextAlignmentCenter it works and it goes in center but I want it align with left size of cell. I tried the CGRectZero instead of CGRectMake(20, 0, 320, 40), changed x but it didn't work!
Any help greatly appreciated.
At first make view with frame (0, 0, 320, 40). Then put label on it.