Shifting leftView of UITextField - ios

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.

Related

Cant center UILabel in titleView of navigationItem iOS8

Im having trouble setting a programmatically allocated UILabel to be centered in my NavigationItem's titleView. Im even setting the NSAlignment to be NSAlignmentCenter. Here's an image of what it looks like:
As you can see.. I have no right UIBarButtonItem in use, but this shouldn't be keeping the text from being centered? Here's the code, in the viewDidLoad:
//Set the title of navBar----------
UILabel *navTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 40)]; //0 0 320 10
navTitleLabel.backgroundColor = [UIColor clearColor];
navTitleLabel.textAlignment = NSTextAlignmentCenter;
navTitleLabel.textColor=[UIColor whiteColor];
navTitleLabel.text = #"IMG";
[navTitleLabel setFont: [UIFont fontWithName:#"BullettoKilla" size:16]];
self.navigationItem.titleView = navTitleLabel;
What am I doing wrong?
As pointed out by 0yeoj you have set the item width to 320. Try something smaller.
Try this
UILabel *navTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 50, 40)];

iOS - Resize UISwitch in UINavigationBar

I am trying to use a UISwitch in my UINavigationBar. I want a smaller switch than the default, so I am scaling the size down with anonymousSwitch.transform = CGAffineTransformMakeScale(0.55, 0.55); When I do this, the switch shrinks, but I cannot figure out how to align it vertically with the other elements in my UINavigationBar.
Here is what it looks like currently:
Ideally, the UISwitch would be spaced away from the 175 UILabel as much as the X UIButton is spaced from the 175 UILabel, and the UISwitch would be in a straight line with the rest of the elements in the UINavigationBar. I tried doing switch.center = CGPointMake(switch.center.x, shareButton.center.y), but this did not affect the placement of the UISwitch at all.
Is there any way for me to position the switch like I want?
I had the same problem.
I tried to solve it and came up with this solution.
I don't think it's a perfect solution and might not work on every iOS-Version.
First you need to subclass UISwitch like this:
#interface UICustomSwitch : UISwitch
#property (assign, nonatomic) CGFloat scale;
#end
#implementation UICustomSwitch
- (void)setFrame:(CGRect)frame {
CGFloat superview_height = self.superview.frame.size.height;
frame.origin.y = (superview_height - frame.size.height * _scale) / 2;
[super setFrame:frame];
}
#end
The second thing to do is to create and setup your UICustomSwitch like that:
UICustomSwitch* switch = [UICustomSwitch new];
switch.transform = CGAffineTransformMakeScale(0.75, 0.75);
switch.scale = 0.75;
UIBarButtonItem* button = [[UIBarButtonItem alloc] initWithCustomView:switch];
To create a bigger space between the 175 and the switch you could just add another UIBarButtonItem between the 175 and the switch. E.g. a fixed spacer with a specific width.
Like i said, i don't think it's a flawless solution.
In my tests it worked.
Another solution by 0yeoj (but a little bit adjusted -> always centered for every navigationBar height, for toolbars it's the same, just use self.navigationController.toolbar.frame.size.height ):
UIView* switch_container = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 44, self.navigationController.navigationBar.frame.size.height)];
UISwitch* switch = [[UISwitch alloc] initWithFrame:switch_container.bounds];
switch.transform = CGAffineTransformMakeScale(0.75, 0.75);
switch.center = CGPointMake(switch_container.bounds.size.width/2, switch_container.bounds.size.height/2);
[switch addTarget:self action:#selector(action:) forControlEvents:UIControlEventValueChanged];
[switch_container addSubview:switch];
UIBarButtonItem* button = [[UIBarButtonItem alloc] initWithCustomView:switch_container];
self.navigationItem.rightBarButtonItem = button;
Have you tried moving switch with another transform method like,
CGAffineTransformMakeTranslation(xValue, yValue);
also you can merge two transform together and give that transform to switch like,
CGAffineTransform scale= CGAffineTransformMakeScale(0.55, 0.55);
CGAffineTransform translate = CGAffineTransformMakeTranslation(0, 10);
anonymousSwitch.transform = CGAffineTransformConcat(scale, translate);
This is the code i tested..
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIButton *xbutton = [UIButton buttonWithType:UIButtonTypeCustom];
xbutton.frame = CGRectMake(0, 6, 35, 35);
xbutton.backgroundColor = [UIColor redColor];
UIView *containerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 44, 44)];
UISwitch *switchButton = [[UISwitch alloc] init];
switchButton.frame = CGRectMake(0, 0, 35, 35);
switchButton.transform = CGAffineTransformMakeScale(0.55, 0.55);
switchButton.center = CGPointMake(containerView.frame.size.width/2, containerView.frame.size.height/2);
[containerView addSubview:switchButton];
UIBarButtonItem *additionalButton = [[UIBarButtonItem alloc] initWithCustomView:containerView];
UIBarButtonItem *xBarButton = [[UIBarButtonItem alloc] initWithCustomView:xbutton];
//rearrange it if necessary
NSArray *arrayButton = [NSArray arrayWithObjects: xBarButton, additionalButton, nil];
self.navigationItem.leftBarButtonItems = arrayButton;
}.
hmm... i haven't DOWNGRADED yet to yosemite so i dont have ios8.3 here this code works perfectly for me though..

UISegmentedControl setFrame not working

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;**

Programmatically add a UISegmentedControl to a UINavigationBar

How do I PROGRAMMATICALLY add a UISegmentedControl to a UINavigationBar?
I do not want to use a XIB file for this.
I have a UIView with a UITableView that is added as a subview.
I have tried two methods but both are not satisfactory:
1)
self.segmentedControl = [[UISegmentedControl alloc]initWithItems:[NSArray arrayWithObjects:#"All",#"Subject",#"Category",#"Finished",nil]];
self.segmentedControl.backgroundColor = [UIColor cloudsColor];
[self.segmentedControl setSelectedSegmentIndex:0];
[self.segmentedControl addTarget:self action:#selector(segmentedControlHasChangedValue) forControlEvents:UIControlEventValueChanged];
self.mainView.tableHeaderView = self.segmentedControl;
The reason this first one fails is that in the UITableView, when the user scrolls, the segmented control is scrolled as well! I don't want that to happen. It must stay pinned to the top.
2) Second attempt
UISegmentedControl *statFilter = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:#"Filter_Personnal", #"Filter_Department", #"Filter_Company", nil]];
[statFilter setSegmentedControlStyle:UISegmentedControlStyleBar];
[statFilter sizeToFit];
self.navigationItem.titleView = statFilter;
This removes my title!!! I have a title at the top in the UINavigationBar and this method removes it!!
Here's an example of what I want to accomplish: UISegmentedControl below UINavigationbar in iOS 7
The UISegmentedControl must stay pinned below as part of the UINavigationBar and it must be below the title!
Thanks!
Use tableView:viewForHeaderInSection: (and possibly tableView:heightForHeaderInSection:) instead of tableHeaderView. Set tableStyle to UITableViewStylePlain (this should be the default).
You can use the following code :
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
if (section == 0) {
UIView *viewHeader = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 100)];
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc]initWithItems:[NSArray arrayWithObjects:#"All",#"Subject",#"Category",#"Finished",nil]];
segmentedControl.backgroundColor = [UIColor cloudsColor];
[segmentedControl setSelectedSegmentIndex:0];
[segmentedControl addTarget:self action:#selector(segmentedControlHasChangedValue) forControlEvents:UIControlEventValueChanged];
segmentedControl.frame = CGRectMake(0, 20, viewHeader.frame.size.width, 50);
[viewHeader addSubview:segmentedControl];
return viewHeader;
}
return nil;
}
Hope this helps.
Thanks.
As the other posters have suggested, you could put the segmented control above the table view and under the nav bar, but you'd need to shift down the table view.
...Or, you could add the segmented control as a tableHeaderView.
A third option is to actually add it to the navigation bar. To do that you have to turn it into a navBarItem. Something like this:
UISegmentedControl *statFilter = [[UISegmentedControl alloc]
initWithItems:
[NSArray arrayWithObjects:
#"Filter_Personnal",
#"Filter_Department",
#"Filter_Company",
nil]];
[statFilter setSegmentedControlStyle:UISegmentedControlStyleBar];
[statFilter sizeToFit];
UIBarItem *theBarItem = [[UIBarItem alloc] initWithCustomView: statFilter];
self.navigationItem.rightBarButtonItem = theBarItem;

How to set UISegmentedControl frame?

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

Resources