Unable to switch between segments for UISegment Control - objective-c

Below is the code snippet I am using to set up my UISegment Control
//Add UIView below the nav bar
UIView *buttonContainer = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 30)];
[self.view addSubview:buttonContainer];
//Set up segment control
NSString *nicknameLabel = [NSString stringWithFormat:#"%#",self.nickname];
UISegmentedControl *tempSegmentControl = [[UISegmentedControl alloc]initWithItems:[NSArray arrayWithObjects:nicknameLabel,#"Friends", #"Everyone", nil]];
tempSegmentControl.frame = CGRectMake(-8, 44, 336, 30);
self.segmentControl = tempSegmentControl;
[self.segmentControl setWidth:112 forSegmentAtIndex:0];
[self.segmentControl setWidth:112 forSegmentAtIndex:1];
[self.segmentControl setWidth:112 forSegmentAtIndex:2];
self.segmentControl.selectedSegmentIndex = 0;
[self.segmentControl addTarget:self action:#selector(toggleControls:) forControlEvents:UIControlEventValueChanged];
[self.segmentControl setSegmentedControlStyle:UISegmentedControlStylePlain];
[self changeSegmentFontSize];
[tempSegmentControl release];
[buttonContainer addSubview:self.segmentControl];
[self.view bringSubviewToFront:buttonContainer];
[buttonContainer bringSubviewToFront:self.segmentControl];
[buttonContainer release];
However, I in the app, I am unable to switch between the segments for the segmented control. (only show default first segment highlighted)
Any advice on how I can fix this?

It looks like a bounds problem. The containing view has bounds {0, 0, 320, 30} while the segmented control has {-8, 44, 336, 30}. This means the segmented control is technically "outside" its containing view and the iOS hit test will not register a touch.
Try not setting the segmented control's frame (temporarily comment out the tempSegmentControl.frame = CGRectMake(-8, 44, 336, 30); line) and see what happens.

Check that userInteractionEnabled is set to TRUE for your view, buttonContainer, and segment control.
Check that the bounds of the buttonContainer's superview are wide and tall enough to fully contain the buttonContainer. If they're not then hit-testing wont work and the segmented control wont get any touches.

Related

UIImageView - UIScrollView Fill and set X to 0

StackOverflow.. last resort as I hate bothering other devs but I've come to the end of my tether with my problem..
I have a UIView, in which are 2 UIScrollViews - in each scrollview is a UIImageView. The original idea is that I can move the image in each scroll view on the Y Axis to reposition the images.
Currently, the UIImageViews are using the content mode of (so the images don't stretch and display in full):
UIViewContentModeScaleAspectFill
The images load fine into the ScrollViews - the only problem is, the position of the UIImageView's are centred on the Y Axis and I need them to be 0! I've tried setting the UIImageView frame after adding and setting the content mode but nothing.
Here is my code:
- (outletContainer is a UIView)
- (imagePack is an NSMutableArray of UIImages)
UIImageView *tempFirst = [[UIImageView alloc] initWithImage:[imagePack objectAtIndex:0]];
[tempFirst setFrame:CGRectMake(0, 0, outletContainer.frame.size.width, outletContainer.frame.size.height/2)];
[tempFirst setContentMode:UIViewContentModeScaleAspectFill];
// add scrollview for positioning:
UIScrollView *firstScroller = [[UIScrollView alloc] init];
[firstScroller setFrame:CGRectMake(0, 0, tempFirst.image.size.width, outletContainer.frame.size.height/2)];
[firstScroller setContentSize:tempFirst.image.size];
[firstScroller addSubview:tempFirst];
[outletContainer addSubview:firstScroller];
UIImageView *tempSecond = [[UIImageView alloc] initWithImage:[imagePack objectAtIndex:1]];
[tempSecond setFrame:CGRectMake(0, 0, outletContainer.frame.size.width, outletContainer.frame.size.height/2)];
[tempSecond setContentMode:UIViewContentModeScaleAspectFill];
// add scrollview for positioning:
UIScrollView *secondScroller = [[UIScrollView alloc] init];
[secondScroller setFrame:CGRectMake(0, outletContainer.frame.size.height/2, tempSecond.image.size.width, outletContainer.frame.size.height/2)];
[secondScroller setContentSize:tempSecond.image.size];
[secondScroller addSubview:tempSecond];
[outletContainer addSubview:secondScroller];
Thanks in advance!
I think you have to delete these two lines:
[tempFirst setFrame:CGRectMake(0, 0, outletContainer.frame.size.width, tempFirst.image.size.height*tempF)];
[tempSecond setFrame:CGRectMake(0, 0, outletContainer.frame.size.width, outletContainer.frame.size.height/2)];
When you create an UIImageView with image it takes UIImage size.
Edited:
Ok, then you have to do that:
[tempFirst setFrame:CGRectMake(0, 0, outletContainer.frame.size.width, tempFirst.image.size.height*outletContainer.frame.size.width/tempFirst.image.size.width)];
firstScroller.contentSize = CGSizeMake(outletContainer.frame.size.width, tempFirst.frame.size.height);

UIButton is moving up when navigation bar hidden

I am adding the one UIButton in bottom of view -
CGRect frame = [UIScreen mainScreen].applicationFrame;
UIButton *menu_btn = [[UIButton alloc] initWithFrame:CGRectMake(xOffset, frame.size.height - 55, 55, 55)];
[self.view addSubview:menu_btn];
And in the view i have UITableview and i am hiding the uinavigation bar when scrolling down in the list and showing when scrolling up.
menu_btn is moving up and down with the list, I want to fix this button.
Anyone please suggest me,how i can achieve this ?
I added in navigationController.view and its working fine. Thanks guys.
CGRect frame = [UIScreen mainScreen].applicationFrame;
UIButton *menu_btn = [[UIButton alloc] initWithFrame:CGRectMake(xOffset, frame.size.height - 55, 55, 55)];
[self.navigationController.view addSubview:menu_btn];

iOS: Programmatically create labels inside scroll view created programmatically

I am developing a project in which I have created scroll view and labels inside scroll view programmatically using following code:
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 44, self.view.frame.size.width, 240)];
int bx=0;
for (NSString * str1 in arr1)
{
if ([arr1 count]<=2) {
label=[[UILabel alloc]initWithFrame:CGRectMake(bx, -1, scrollView.frame.size.width/[arr1 count], 100)];
label.textAlignment=NSTextAlignmentCenter;
}
else
{
label=[[UILabel alloc]initWithFrame:CGRectMake(bx, -1, 106, 100)];
label.textAlignment=NSTextAlignmentCenter;
}
[label setText:[NSString stringWithFormat:#"(%#)",str1]];
[scrollView addSubview:label];
bx +=label.frame.size.width;
}
I want to create another line of labels below the above created labels using new array.and also I want to give different color to each label.
I don't know if I really understood, but maybe you should try to play with the second property of your CGRectMake which is for the Y value.

Changing the size of a Round Button in Cocoa

Is there a way by which I can increase the size of a Round Button in Cocoa? I am doing a Cocoa Application in which one of the view contains some of the user's avatars. I would like to use a Round Button and set the images to it. But I can't find any way to increase the size of the Round Button.
Is there some way of doing it?
yourButton.frame = CGRectMake(0, 0, 20, 20);
//or
yourButton.frame = CGRectMake(0, 0, 40, 60);
//or
yourButton.frame = CGRectMake(0, 0, 100, 80);
To resize the Rounded Rect NSButton you need to customize and you need to draw your own button.
To solve your problem use Gradient button resize what you want and setImage:an image should be rounded rect. then make borderless button
[button setBordered: NO];
now it will appear like rounded rect button.
To remove gray highlight use
[[button cell] setHighlightsBy:0];
Try this...
UIButton *myButton = [[UIButton alloc] initWithFrame:CGRectMake(50, 50, 150, 60)];
myButton.enabled = YES;
myButton.backgroundColor = [UIColor lightGrayColor];
[myButton setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
[myButton setTitle:#"My Button" forState:UIControlStateNormal];
[self.view addSubview:myButton];
Change the 150 and 60 values to alter the shape of your button.
In case you are using images, they try with UIButton's method setBackgroundImage:forState:.
With this method, whenever you change the frame, it will adjust the image according to that.

UITableViewCell: Accessory View going away when selected

I might be going about this all wrong, so hopefully someone will tell me what I should be doing.
I have constructed a table view to act as a legend and color picker. In the AccessoryView slot, I places a custom view that is just a colored box. User will show the legend, so they know what colors mean what, and they can pick on a color to then 'colorize' a drawing by assigning that color to objects. All of that works just fine.
What I'm having a problem with is the selected row style. When I select a row from the legend, the cell goes blue, like it should, but my AccessoryView has now disappeared. I don't want this to happen. However, I don't know what to set to make it not disappear. Keep in mind, I still want a row to show up as 'selected'. But any way that I do that, my accessory view disappears (most likely is hidden by the selected color).
Here's how I'm setting the accessory view right now.
CGRect colorBox = CGRectMake(0, 0, 30, 30);
UIView *colorView = [[UIView alloc] initWithFrame:colorBox];
colorView.backgroundColor = [self colorWithHexString:[selOption valueForKey:#"qoColor"]];
cell.accessoryView = colorView;
You can use a UIImageView instead of a UIView, which won't disappear when the cell is selected. You can either make tiny .png thumbnails for each color (if there aren't a lot) or you can create them dynamically in your cellForRowAtIndexPath delegate method, ala:
UIImageView *colorView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
CGRect rect = CGRectMake(0.0f, 0.0f, 30.0f, 30.0f);
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [[self colorWithHexString:[selOption valueForKey:#"qoColor"]]; CGColor]);
CGContextFillRect(context, rect);
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[colorView setImage:image];
[cell setAccessoryView:colorView];
[colorView release];