The Notes app uses a small image of ragged paper to imply prior torn pages.
I tried to replicate this behaviour in the following way, but my problem is, the search bar doesn't become active anymore.
//mySearchBar and raggedPaper are instance variables
mySearchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0,0,320,44)];
mySearchBar.delegate = self;
[mySearchBar sizeToFit];
raggedPaper = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"raggedpaper.png"]];
[raggedPaper addSubView:mySearchBar];
self.tableView.tableHeaderView = raggedPaper;
Everything looks alright, but the searchbar doesn't activate anymore.
Did you try this :
raggedPaper.userInteractionEnabled = YES
Or if it doesn't work, you can do the other way around : add the ragged paper image to the search bar, whose clipsToBounds is set to NO.
Related
I'm trying to add a UISegmentedControl within the title of a UINavigationController. However, the formatting looks like this (i.e. its ugly).
When I want it to look like this (pretty :). Can anyone help??
I've read the popular example by Red Artisan here. But I'm not showing this as my first view (like Red Artisan does), so I've moved a lot of the code out of App Delegate. In App Delegate, I do set up this screen to be a UINavigationController with its rootView a UIViewController.
GenInfoViewController *genInfoController = [[GenInfoViewController alloc] initWithNibName:#"GenInfoViewController" bundle:nil];
UINavigationController *genInfoNavController = [[UINavigationController alloc] initWithRootViewController:genInfoController];
Then in viewDidLoad of GenInfoViewController.m I do the following:
self.segmentedControl = [[UISegmentedControl alloc] initWithItems:#[#"Info",#"Map"]];
self.navigationItem.titleView = self.segmentedControl;
To style a segmented control, set the segmentedControlStyle property to one of the following:
UISegmentedControlStylePlain
UISegmentedControlStyleBordered
UISegmentedControlStyleBar
UISegmentedControlStyleBezeled
For example:
self.segmentedControl = [[UISegmentedControl alloc] initWithItems:#[#"Info",#"Map"]];
self.segmentedControl.segmentedControlStyle = UISegmentedControlStyleBordered;
self.navigationItem.titleView = self.segmentedControl;
There's some relevant Q+As on here regarding styling segment controls:
Custom segment control
Remove rounded corner
Change font size
Change colour of selected segment
If you'd like to try a custom segmented control, check out all the available CocoaControls and CocoaPods.
Yup, you need to set the property "segmentedControlStyle" on your UISegmented control.
Your options are as follows:
typedef enum {
UISegmentedControlStylePlain,
UISegmentedControlStyleBordered,
UISegmentedControlStyleBar, // This is probably the one you want!
UISegmentedControlStyleBezeled,
} UISegmentedControlStyle;
So the following should probably do the trick:
self.segmentedControl = [[UISegmentedControl alloc] initWithItems:#[#"Info",#"Map"]];
self.segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
self.navigationItem.titleView = self.segmentedControl;
One thing you might also want to consider is setting the "tintColor" of the segmented control too.
self.segmentedControl = [UIColor blackColour];
Will leave you with something like this:
Obviously there is lots of other customisation you can do too. Take a look at the documentation here: http://developer.apple.com/library/ios/#documentation/uikit/reference/UISegmentedControl_Class/Reference/UISegmentedControl.html
My search bar is stretched slightly too far right when you start in landscape mode. It is still slightly too wide if you then rotate to portrait mode. However, its fine if you start in portrait mode and also if you then rotate it to landscape. Here is my code.
sBar = [[UISearchBar alloc] initWithFrame:CGRectZero];
[sBar sizeToFit];
sBar.delegate = self;
sBar.autoresizingMask = UIViewAutoresizingFlexibleWidth |UIViewAutoresizingFlexibleBottomMargin;
[self.view addSubview:sBar];
The answer was to add the search bar to the view before running the rest of the code.
UISearchBar * tempSBar = [[UISearchBar alloc] initWithFrame:CGRectZero];
self.sBar = tempSBar;
[tempSBar release];
[self.view addSubview:sBar];
[sBar sizeToFit];
sBar.delegate = self;
sBar.autoresizingMask = UIViewAutoresizingFlexibleWidth |UIViewAutoresizingFlexibleBottomMargin;
UIViewAutoresizingFlexibleRightMargin
add that to sBar.autoresizingMask, i have that aswell as the other 2 and have no issues. if you are using IB, make sure its the right width in there also
I know this is old and deprecated, but I just had to share. I looked high and low for a solution that would work for my case. I tried all the suggestions, to no avail. Then I stumbled across this gem
// Update search bar frame.
CGRect superviewFrame = self.searchDisplayController.searchBar.superview.frame;
superviewFrame.origin.y = 0.f;
self.searchDisplayController.searchBar.superview.frame = superviewFrame;
And everything became nice and sunny again. Thank you Peter at
http://petersteinberger.com/blog/2013/fixing-uisearchdisplaycontroller-on-ios-7/
I am trying to display a UISearchBar in place of the button shown on the right side of the UINavigationItem. I am using this code:
- (void)viewDidLoad {
[super viewDidLoad];
UISearchBar *searchBar = [[UISearchBar alloc] init];
UIBarButtonItem *navRight = [[UIBarButtonItem alloc] initWithCustomView:searchBar];
[[self navigationItem] setRightBarButtonItem:navRight];
[searchBar release];
[navRight release];
}
However, the displayed search bar is only a few pixels wide. I need to make it wider.
Am I going about this the wrong way? Is it at all possible?
You need to set the UISearchBar's frame at some point.
e.x.
UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 200, 20)];
EDIT: Regarding the button-esque artifact under the UISearchBar
The button-y thing that you see under the UISearchBar is actually a background element of the UISearchBar, not the result of the search bar overlapping a UIBarButtonItem. Since there doesn't seem to be a nice way to hide this (and I hope someone can come along and correct me), I can only point you in the direction of a hack that comes with the usual perils of SDK version dependence, inelegance, and potential for more headaches.
EDIT 2: Another potential solution
You could also set your UISearchBar's frame's height to 44 (the height of the navbar) and then the background should blend nicely with the navbar. Based off this SO post I recently saw.
I'm using the following code in my root view controller class implementation:
- (void)viewDidLoad
{
[super viewDidLoad];
NSArray *segmentTextContent = [NSArray arrayWithObjects:
NSLocalizedString(#"button1", #""),
NSLocalizedString(#"button2", #""),
NSLocalizedString(#"button3", #""), nil];
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] nitWithItems:segmentTextContent];
segmentedControl.selectedSegmentIndex = 0;
segmentedControl.autoresizingMask = UIViewAutoresizingFlexibleWidth;
segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
segmentedControl.frame = CGRectMake(0, 0, 400, 30);
self.navigationItem.titleView = segmentedControl;
[segmentedControl release];
}
The segmented control is not centered correctly, it is shifted a few pixels to the right. The funny thing is, it is not right-aligned either. It's as if there is some left padding preventing it from being centered. Another funny thing is, if I draw this segmented control in interface builder, it centers perfectly, so this is only a programmatic issue.
I've tried this code in a brand new empty app and get the same results, so I know it's not something else in my app that is causing it.
Sorry, I wanted to post a screen shot, but I don't have enough reputation points ;-)
Any Ideas?
There is no left button, I've even tried doing:
self.navigationItem.leftBarButtonItem = nil;
self.navigationItem.backBarButtonItem = nil;
As I said, it works in IB, just not in code. And it also fails in a brand new project - you can try it for yourself, and see...
I seemed to have found the fix. The 400 specified for the width was causing the problem. I specified 300, and now it is centered correctly. I supposed ideally I should be grabbing some global screen width value instead of hard-coding 300.
I am using the following sample code to add a link to a UITextView in a UITableViewCell, but it is not creating a link. I have done this before without issues... Has something changed with the recent iOS upgrade?
UITextView *myView = [[UITextView alloc] initWithFrame: frame];
myView.text = #"this is http://google.com link";
myView.editable = NO;
myView.dataDetectorTypes = UIDataDetectorTypeLink;
//cell is the TableView's cell
[cell.contentView addSubview:myView];
[myView release];
tried simply putting this code in a subview on a normal view and works great for me (iOS 4.2), maybe the bug is somewhere else (or maybe frame is empty)
//EDIT: and tried for content view in TableViewCell, works also great
Just to have the question (maybe) solved: you have to reset myView.dataDetectorTypes = UIDataDetectorTypeLink; every time you reload the table.