What is UISegmentedControl image size - ios

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.

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)];

Adding a UISegmentedControl to UITableView

How do I add a UISegmentedControl to a UITableView?
Similar to the image found here:
http://cdn.imore.com/sites/imore.com/files/styles/large/public/field/image/2012/08/iHomework-for-iPhone-managing-assignments-and-tasks.jpg
I would like to have a UISegmentedControl that sticks underneath the navigation bar.
This is what I've tried so far:
In my viewDidLoad method, I've created a UISegmentedControl
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc]initWithItems:[NSArray arrayWithObjects:#"One", #"Two", nil]];
segmentedControl.frame = CGRectMake(50, 0, 220, 100);
[segmentedControl addTarget:self action:#selector(segmentedControlHasChangedValue:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:segmentedControl];
However, the segmented control overlaps with the tableview itself.
EDIT
Additionally,I would like to create this uisegmentedcontrol fully programmatically as I don't want to use a nib file.
self.tableView.tableHeaderView = segmentedControl;
If you want it to obey your width and height properly though enclose your segmentedControl in a UIView first as the tableView likes to mangle your view a bit to fit the width.

How to present 'UIViewController' in Cocoas2D with custom size

Imagine I have dialog and size of this dialog is 200x200 and I want to present on the iPhone screen temporary (can dismiss with button).
I try it. but the view controller that I presented is replace current scene (dialog and white background border).
I want to present with custom size (200x200) at center of iPhone screen and see my content as background
I try to set dialog's background colour to clearColor and It doesn't work.
Thanks in advance.
Create your button like this,
[button addTarget:self action:#selector(ButtonFun) forControlEvents:UIControlEventTouchUpInside];
-(void)ButtonFun {
UIView *newView1 = [[UIView alloc] initWithFrame:CGRectMake(0,0,200,200)];
newView1.backgroundColor = [UIColor whiteColor];
UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];
textView.text = #"Write your dialog";
[textView setFont:[UIFont fontWithName:#"ArialMT" size:15]];
textView.textAlignment = NSTextAlignmentCenter;
[newView1 addSubview:textView];
newView1.center = self.view.center;
[self.view addSubview:newView1];
}
To remove this UIView use UITapGestureRecognizer,or simply
[newView1 removeFromSuperview];

Adding a UIImageView as a subview of UILabel, image not appearing

So, when I do this with a regular old view:
UIView *topBlock = [[UIView alloc] initWithFrame:CGRectMake(0,-frameSize.height, frameSize.width, frameSize.height/2)];
[viewController.view addSubview:topBlock];
topBlock.autoresizesSubviews = NO;
topBlock.clipsToBounds = YES;
UIImage *topImage = [UIImage imageNamed:#"BloktLayout"];
UIImageView *topImageView = [[UIImageView alloc] initWithImage:topImage];
topImageView.frame = viewController.view.frame;
[topBlock addSubview:topImageView];
I get the nice old image where I want it, in the top view. But the middle view is a UILabel, and when I try the same thing:
UILabel *midBar = [[UILabel alloc] initWithFrame:CGRectMake(midBarOrigin.x, midBarOrigin.y, midBarWidth, midBarHeight)];
midBar.text = #"Blokt";
midBar.textColor = [UIColor blackColor];
midBar.textAlignment = NSTextAlignmentRight;
midBar.font = [UIFont fontWithName:#"HelveticaNeue-UltraLight" size:80.0f];
[viewController.view addSubview:midBar];
midBar.autoresizesSubviews = NO;
midBar.clipsToBounds = YES;
UIImage *midImage = [UIImage imageNamed:#"BloktLayout"];
UIImageView *midImageView = [[UIImageView alloc] initWithImage:midImage];
midImageView.frame = viewController.view.frame;
[midBar addSubview:midImageView];
I don't see any image at all in the UILabel. Any help?
Seems like the issue is related to your frames.
Tough to say without additional info. Can you post viewController.view.frame, frameSize, and midBarOrigin / midBarWidth / midBarHeight?
In the second codeblock, midBar.clipsToBounds == YES, but it looks like the midImageView.frame is likely very different / outside of midBar.frame in which case it wouldn't be visible.
Edit Some screenshots would help but aren't necessary
Edit 2 Note that subviews' origin points are always relative to the coordinate system of their superview, never relative to the coordinate system of any other view in the view heierarchy. This is likely the heart of the issue here. If you do want to convert CGPoints or CGRects from one coordinate system to another there are methods on UIView such as convertRect: and convertPoint: etc.
Interface Builder doesn't even let you add a control inside of a UILabel.
Instead, if you wish to group multiple controls, you can add them both as subviews of a UIView.
In other words, your image view and label can share the same superview, but the image view cannot be a subview of the label.
If they share the same superview, you can position the image view behind the label, and it should appear "through" the label as long as the label's background is clear.
Simple Way to do.....
You can add UIImageView, UILabel as subview of cell.textLabel
UIImageView *statusImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 4, 8, 8)];<br/>statusImage.backgroundColor = [UIColor redColor];<br/>
statusImage.layer.cornerRadius = 4;<br/>
statusImage.clipsToBounds = YES;<br/>
[cell.textLabel addSubview:statusImage];<br/>
UILabel *Lbl = [[UILabel alloc] initWithFrame:CGRectMake(15, 0, cell.textLabel.frame.size.width - 15, cell.textLabel.frame.size.height)];<br/>
Lbl.text = #"xyz";<br/>
[cell.textLabel addSubview:Lbl];<br/>
I just had this problem as well.
I found that ImageView was behind the label.
When I replaced label with UIView, it works properly.

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