I wanted to customize UISegmentControl of my app and I used following codes:
UIImage *segmentSelected = [UIImage imageNamed:#"segcontrol_sel.png"];
UIImage *segmentUnselected = [UIImage imageNamed:#"segcontrol_uns.png"];
UIImage *segmentSelectedUnselected = [UIImage imageNamed:#"segcontrol_sel-uns.png"];
UIImage *segUnselectedSelected = [UIImage imageNamed:#"segcontrol_uns-sel.png"];
UIImage *segmentUnselectedUnselected = [UIImage imageNamed:#"segcontrol_uns-uns.png"];
[[UISegmentedControl appearance] setBackgroundImage:segmentUnselected
forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
[[UISegmentedControl appearance] setBackgroundImage:segmentSelected
forState:UIControlStateSelected barMetrics:UIBarMetricsDefault];
[[UISegmentedControl appearance] setDividerImage:segmentUnselectedUnselected
forLeftSegmentState:UIControlStateNormal
rightSegmentState:UIControlStateNormal
barMetrics:UIBarMetricsDefault];
[[UISegmentedControl appearance] setDividerImage:segmentSelectedUnselected
forLeftSegmentState:UIControlStateSelected
rightSegmentState:UIControlStateNormal
barMetrics:UIBarMetricsDefault];
[[UISegmentedControl appearance]
setDividerImage:segUnselectedSelected
forLeftSegmentState:UIControlStateNormal
rightSegmentState:UIControlStateSelected
barMetrics:UIBarMetricsDefault];
I got following pictures from my app
I used several methods to correct the problem like CGImages and Resizable Images but none of them worked! What is the problem and How should I solve it?
I think you should have look at the link below
http://idevrecipes.com/2010/12/13/wooduinavigation/
and there is one demo code also available here
https://github.com/xhan/PlutoLand
Related
I use this code for customization UIBarButtonItems:
backImage = [UIImage imageNamed:#"barButtonImage"];
backImage = [backImage resizableImageWithCapInsets:UIEdgeInsetsMake(0.0f, 6.0f, 0.0f, 6.0f) resizingMode:UIImageResizingModeTile];
[[UIBarButtonItem appearance] setBackButtonBackgroundImage:backImage forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
[[UIBarButtonItem appearance] setBackgroundImage:backImage forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
All of navigation bars look fine
But toolbars have resized items in strange way:
All of UIBarButtonItems are defined in storyboard without any custom views. How can I remove this resizing?
I'm customizing the elements like navigation bar, tab bar and uibarbuttonitem using UIAppearance. It works very well except for a very strange behavior of the UIBarButtonItem elements. On the top level of the navigation controller hierarchy everything looks good, but if I push the next view controller, the UIBarButtonItem elements move a little bit downwards, but at the same time, the back button stays at its correct position. I attached two images to illustrate my problem.
1) First view controller in the navigation controller hierarchy
2) Second view controller in the navigation controller hierarchy
EDIT: The code
//Change navigation bar appearance
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:#"GPNavigationBarBackground.png"] forBarMetrics:UIBarMetricsDefault];
[[UINavigationBar appearance] setShadowImage:[[UIImage alloc] init]];
[[UINavigationBar appearance] setShadowImage:[UIImage imageNamed:#"GPNavigationBarShadow.png"]];
[[UINavigationBar appearance] setTitleTextAttributes:
[NSDictionary dictionaryWithObjectsAndKeys:
[UIColor whiteColor], UITextAttributeTextColor,
[UIFont boldSystemFontOfSize:17], UITextAttributeFont,nil]];
[[UINavigationBar appearance] setTitleVerticalPositionAdjustment:2.f forBarMetrics:UIBarMetricsDefault];
UIImage *buttonBackground = [[UIImage imageNamed:#"GPNavigationBarButton.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 10, 0, 10)];
UIImage *buttonPressedBackground = [[UIImage imageNamed:#"GPNavigationBarButtonPressed.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 10, 0, 10)];
UIImage *backButtonBackground = [[UIImage imageNamed:#"GPNavigationBarBackButton.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 15, 0, 10)];
UIImage *backButtonPressedBackground = [[UIImage imageNamed:#"GPNavigationBarBackButtonPressed.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 15, 0, 10)];
[[UIBarButtonItem appearance] setBackgroundImage:buttonBackground forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
[[UIBarButtonItem appearance] setBackgroundImage:buttonPressedBackground forState:UIControlStateHighlighted barMetrics:UIBarMetricsDefault];
[[UIBarButtonItem appearance] setBackButtonBackgroundImage:backButtonBackground forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
[[UIBarButtonItem appearance] setBackButtonBackgroundImage:backButtonPressedBackground forState:UIControlStateHighlighted barMetrics:UIBarMetricsDefault];
[[UIBarButtonItem appearance] setBackButtonBackgroundVerticalPositionAdjustment:1.f forBarMetrics:UIBarMetricsDefault];
[[UIBarButtonItem appearance] setBackgroundVerticalPositionAdjustment:1.f forBarMetrics:UIBarMetricsDefault];
[[UIBarButtonItem appearance] setTitlePositionAdjustment:UIOffsetMake(0, 1.f) forBarMetrics:UIBarMetricsDefault];
Well, after brooding on that problem, I finally solved it. The code above is absolutely correct. The only problem was the height of the background images for the UIBarButtonItem. The UIAppearance proxy lets you set all the graphics, but it doesn't allow you to alter the height of the UIBarButtonItem.
So, when customizing UIBarButtonItem, always remember that a
UIBarButtonItem can't be higher than 30pt. That means that custom artwork shouldn't exceed this size.
You can set background images that are higher than 30pt, but you will run into the same problem as stated above.
I have a tableviewcontroller, specifically, in my app which is getting a little jittery when scrolling.
I fairly certain it's because of my custom UISegmentedControl appearance, I have four in my table, each in their own cell.
I use this code in my app delegate to customise them:
[[UISegmentedControl appearance] setBackgroundImage:[[UIImage imageNamed:#"SegmentedControl.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 4, 0, 4)] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
[[UISegmentedControl appearance] setBackgroundImage:[[UIImage imageNamed:#"SegmentedControlSelected.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 4, 0, 4)] forState:UIControlStateSelected barMetrics:UIBarMetricsDefault];
[[UISegmentedControl appearance] setDividerImage:[UIImage imageNamed:#"SegmentedControlDivider.png"] forLeftSegmentState:UIControlStateNormal rightSegmentState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
[[UISegmentedControl appearance] setDividerImage:[UIImage imageNamed:#"SegmentedControlSelectedDivider.png"] forLeftSegmentState:UIControlStateSelected rightSegmentState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
[[UISegmentedControl appearance] setDividerImage:[UIImage imageNamed:#"SegmentedControlSelectedDivider.png"] forLeftSegmentState:UIControlStateNormal rightSegmentState:UIControlStateSelected barMetrics:UIBarMetricsDefault];
[[UISegmentedControl appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
[UIColor whiteColor], UITextAttributeTextColor,
[UIColor grayColor], UITextAttributeTextShadowColor,
[NSValue valueWithUIOffset:UIOffsetMake(0, -1)], UITextAttributeTextShadowOffset,
[UIFont fontWithName:#"Georgia-Italic" size:0.0], UITextAttributeFont,
nil] forState:UIControlStateNormal];
Why might this cause such a slow down? What could I do to improve performance? It jitters as they come onto the screen. I have never had a problem like this before.
I'm fairly sure its the switches and segmented controls, as when I remove them it speeds up and goes all silky smooth. I basically do this:
if (indexPath.row == 0)
{
cell.textLabel.text = #"text";
UISegmentedControl *segControl = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:#"1", #"2", nil]];
[segControl setSelectedSegmentIndex:[myBool boolValue]];
[segControl setSegmentedControlStyle:UISegmentedControlStyleBar];
[segControl addTarget:self action:#selector(myMethod:) forControlEvents:UIControlEventValueChanged];
cell.accessoryView = segControl;
}
In my cellForRowAtIndexPath 7 times, how can I make this faster?
I have an app that I am working on and I had some similar customizations. I recommend reusing the set controls... You can accomplish this by making a UITableViewCell subclass and setting up your seg control in it, then you can create outlets for the things you need to customize.
A quick search found this tutorial and it looks like what I am talking about
UITableViewCell Prototype Tutorial
This way your cells will be completely reusable and therefore, much faster.
But then, I don't now your exact use case, so maybe this won't apply to you. It sped up my cells with custom buttons, art, and animations quite a bit.
Just remember alloc inits are very slow.
I guess another option would be to pre-init all that you can with the controls and maybe store them in and array then only load them in cellForRowAtIndexPath:
I have a UISegmentedControl in my iPad app which I have customised with the new methods available in iOS5, as below:
[[UISegmentedControl appearance] setBackgroundImage:segmentUnselected forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
[[UISegmentedControl appearance] setBackgroundImage:segmentSelected forState:UIControlStateSelected barMetrics:UIBarMetricsDefault];
[[UISegmentedControl appearance] setDividerImage:segmentUnselectedUnselected forLeftSegmentState:UIControlStateNormal rightSegmentState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
[[UISegmentedControl appearance] setDividerImage:segmentSelectedUnselected forLeftSegmentState:UIControlStateSelected rightSegmentState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
[[UISegmentedControl appearance] setDividerImage:segUnselectedSelected forLeftSegmentState:UIControlStateNormal rightSegmentState:UIControlStateSelected barMetrics:UIBarMetricsDefault];
NSDictionary *textAttributes = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:17], UITextAttributeFont,
[UIColor colorWithRed:0.3 green:0.34 blue:0.42 alpha:1], UITextAttributeTextColor,
[UIColor whiteColor], UITextAttributeTextShadowColor,
CGSizeMake(0, 1), UITextAttributeTextShadowOffset, nil];
[[UISegmentedControl appearance] setTitleTextAttributes:textAttributes forState:UIControlStateNormal];
It looks fine and works ok, but there's a problem in that calling setEnabled:NO on any of the segments has no effect - the segment will still respond to touch events. Does anybody know what I need to do to disable some of the segments?
Use isEnabledForSegmentAtIndex: and setEnabled:forSegmentAtIndex: found in the documentation for UISegmentedControl. Let me know if that gets it working.
Not sure if this is still an issue for anyone (since it's fixed in iOS 6, but here's the workaround that worked from me (borrowed from another question):
In your viewDidLoad, try:
dispatch_async(dispatch_get_main_queue(),^{
[self.segmentedControl setEnabled:NO forSegmentAtIndex:2];
});
It seems that when the views load, the presence of an appearance proxy resets the other properties of the UISegmentedControl. Scheduling this on the main thread will re-enable/re-disable. This also BTW works for selecting the default segment.
So I have the following code to set the segmented control background color:
UISegmentedControl * segmentedCtrl = [[UISegmentedControl alloc] initWithFrame:CGRectMake(0, 0, 150, 35)];
[segmentedCtrl setBackgroundImage:[UIImage imageNamed:#"btn-gradient-brown"] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
[segmentedCtrl setBackgroundImage:[UIImage imageNamed:#"btn-gradient-brown"] forState:UIControlStateSelected barMetrics:UIBarMetricsDefault];
[segmentedCtrl insertSegmentWithImage:[UIImage imageNamed:#"icon-home.png"] atIndex:0 animated:NO];
[segmentedCtrl insertSegmentWithImage:[UIImage imageNamed:#"icon-star.png"] atIndex:1 animated:NO];
It all works fine except that when I click on the segment I can see the blue button highlighted. How do I disable this highlight? Here's a screenshot of my problem:
UISegmentControl has a Divider between two segments. By using the following code you can change the divider background.
[segmentedCtrl setDividerImage:[UIImage imageNamed:#"divider_selected.png"] forLeftSegmentState:UIControlStateSelected rightSegmentState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
[segmentedCtrl setDividerImage:[UIImage imageNamed:#"divider_normal.png"] forLeftSegmentState:UIControlStateNormal rightSegmentState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
Where divider_selected.png is a image which you used for selected state. Where as divider_normal.png is a image which you used for normal state.
Hope this will help....
Set your same background images for the highlighted state:
[segmentedCtrl setBackgroundImage:[UIImage imageNamed:#"btn-gradient-brown"] forState:UIControlStateHighlighted barMetrics:UIBarMetricsDefault];