iOS 7 UITableView inside a popover - ios

I have a UITableView which has some custom styling. This table view appears in two places in the app, one of which is inside a UIPopoverController. However when the tableview is inside the popover it takes on the default tableview styling as stated in the UI Transition Guide under "Popover".
The problem I have is that there appears to be nowhere to change this behaviour. Regardless of where I try and modify properties of the tableview the view inside the popover doesn't change.
Anyone dealt with this issue before or have any ideas?
Here is the init method of LibraryProductView where I create the table view:
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.sectionOrdering = [NSArray arrayWithObjects:
[NSNumber numberWithInt:LIBRARY_PRODUCT_SECTION_DESCRIPTION],
[NSNumber numberWithInt:LIBRARY_PRODUCT_SECTION_DOCUMENTS],
[NSNumber numberWithInt:LIBRARY_PRODUCT_SECTION_ACTIVE_INGREDIENTS],
[NSNumber numberWithInt:LIBRARY_PRODUCT_SECTION_RELATED_PRODUCTS],
[NSNumber numberWithInt:LIBRARY_PRODUCT_SECTION_RELATED_DOCUMENTS], nil];
self.backgroundColor = [UIColor whiteColor];
self.tableView = [[UITableView alloc] initWithFrame:CGRectInset(self.bounds, 10, 0) style:UITableViewStyleGrouped];
self.tableView.backgroundColor = [UIColor whiteColor];
self.tableView.dataSource = self;
self.tableView.delegate = self;
self.tableView.separatorColor = [UIColor clearColor];
self.tableView.showsVerticalScrollIndicator = NO;
[self addSubview:self.tableView];
}
return self;
}
Here is where the containing view (LibraryProductView) is added to the popover:
- (IBAction)didTouchInformationButton:(id)sender
{
if (_infoPopover != nil && _infoPopover.isPopoverVisible)
{
[_infoPopover dismissPopoverAnimated:YES];
return;
}
CGSize preferredSize = CGSizeMake(600.0f, 500.0f);
LibraryProductViewController* productController = [[[LibraryProductViewController alloc] initWithPreferredSize:preferredSize] autorelease];
productController.filterByMyCompany = NO;
productController.product = _activityInput.product;
UINavigationController* nav = [[[UINavigationController alloc] initWithRootViewController:productController] autorelease];
nav.title = _activityInput.product.name;
RELEASE(_infoPopover);
_infoPopover = [[UIPopoverController alloc] initWithContentViewController:nav];
_infoPopover.popoverContentSize = CGSizeMake(preferredSize.width, preferredSize.height + 46);
[_infoPopover presentPopoverFromRect:_infoButton.frame inView:_infoButton permittedArrowDirections:UIPopoverArrowDirectionLeft animated:YES];
}
The LibraryProductView is created within viewDidLoad method of LibraryProductViewController.
- (void)viewDidLoad
{
[super viewDidLoad];
self.libraryProductView = [[LibraryProductView alloc] initWithFrame:(usingPreferredSize ? CGRectMake(0.0, 0.0, preferredSize.width, preferredSize.height) : self.view.bounds)];
self.libraryProductView.dataSource = self;
self.libraryProductView.delegate = self;
[self.view addSubview:self.libraryProductView];
}

To set properties for the TableView you might do so in
- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView
{
[tableView setBackgroundColor:[UIColor redcolor]];
[tableView setSeparatorColor: [UIColor blueColor]];
return 1;
}
This, of course, assumes you have set UITableViewDataSource in your .h file

Related

How to use a SearchController.searchBar as a tableview.sectionHeader

question one :
when a SearchController.searchBar as a tableview.tableHeaderView, and tableview have backgroundColor
why it has a strange color: image
question two :
How to use a SearchController.searchBar as a tableview.sectionHeader
when i try do it, my tableView will be so crazy image
code snippet :
- (UISearchController *)searchController{
if (!_searchController) {
_searchController = [[UISearchController alloc] initWithSearchResultsController: self.resultVC];
_searchController.searchResultsUpdater = self;
_searchController.delegate = self;
self.definesPresentationContext = YES;
// 是否添加半透明覆盖
_searchController.dimsBackgroundDuringPresentation = YES;
// 是否隐藏导航栏
_searchController.hidesNavigationBarDuringPresentation = YES;
// 可以通过此种方式修改searchBar的背景颜色
_searchController.searchBar.barTintColor = GL_NAVBAR_COLOR;
UIImageView *barImageView = [[[_searchController.searchBar.subviews firstObject] subviews] firstObject];
barImageView.layer.borderColor = GL_NAVBAR_COLOR.CGColor;
barImageView.layer.borderWidth = 1;
// 可以通过此种方式可以拿到搜索框,修改搜索框的样式
UITextField *searchField = [[[_searchController.searchBar.subviews firstObject] subviews] lastObject];
searchField.backgroundColor = [UIColor yellowColor];
searchField.placeholder = #"请输入搜索内容";
}
return _searchController;
}
- (void)configureUI{
self.tableView = [[UITableView alloc] initWithFrame: self.view.bounds style:UITableViewStylePlain];
self.tableView.delegate = self;
self.tableView.dataSource = self;
[self.view addSubview: self.tableView];
self.tableView.backgroundColor = backgroundColor;
self.searchController.searchBar.frame = CGRectMake(0, 0, ScreenWidth, 44);
#if tableHeaderViewSearchBar
self.tableView.tableHeaderView = self.searchController.searchBar;
#endif
}
#if !tableHeaderViewSearchBar
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
return 50;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
return self.searchController.searchBar;
}
#endif
You can place the UISearchBar of UISearchController in the navigation bar so that it remains fixed at the top.
So, just add below code at the end of configureUI method and remove last 2 lines of code inside the same method.
self.searchController.hidesNavigationBarDuringPresentation = NO;
self.searchController.searchBar.searchBarStyle = UISearchBarStyleMinimal;
self.navigationItem.titleView = self.searchController.searchBar;
self.definesPresentationContext = YES;
I have a provisional idea:
- (void)viewDidLoad {
[super viewDidLoad];
[self.searchController.searchBar sizeToFit];
self.tableView.frame.origin.y = CGRectGetMaxY(self.searchController.searchBar.frame);
[self.view addSubview: self.searchController.searchBar];
}
To answer question one: you need to set tableView.backgroundView
UIView *tableBackgroundView = [[UIView alloc]initWithFrame:self.tableView.bounds];
tableBackgroundView.backgroundColor = GL_BACKGROUD_COLOR;
self.tableView.backgroundView = tableBackgroundView;

Adding UICollectionViewLayout programmatically issue

I'm trying to implements this library without using storyBoard (first step for implementing this library) , because I'm creating my UIcollectionView programmatically.
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
//self.view.backgroundColor = [UIColor whiteColor];
[self.view addSubview:self.collectionView];
[_collectionView registerNib:[UINib nibWithNibName:#"myCell" bundle:nil] forCellWithReuseIdentifier:#"cell3"];
[_collectionView setBackgroundColor:[UIColor colorWithRed:0.945 green:0.945 blue:0.945 alpha:1] ];
[_collectionView setTransform:CGAffineTransformMakeScale(-1, 1)];
RFQuiltLayout* layout = (id)[_collectionView collectionViewLayout];
layout.direction = UICollectionViewScrollDirectionVertical;
layout.blockPixels = CGSizeMake(100, 100);
}
- (UICollectionView *)collectionView {
if (!_collectionView) {
CGRect collectionViewFrame = self.view.bounds;
collectionViewFrame.size.height -= (self.navigationController.viewControllers.count > 1 ? 0 : (CGRectGetHeight(self.tabBarController.tabBar.bounds))) + 0;
// FMMosaicLayout *mosaicLayout = [[FMMosaicLayout alloc] init];
//// _collectionView = [[UICollectionView alloc] initWithFrame:collectionViewFrame collectionViewLayout:mosaicLayout];
// RFQuiltLayout* layout = (id)[_collectionView collectionViewLayout];
// layout.direction = UICollectionViewScrollDirectionVertical;
// layout.blockPixels = CGSizeMake(100, 100);
_collectionView = [[UICollectionView alloc] initWithFrame:collectionViewFrame collectionViewLayout:[[RFQuiltLayout alloc] init]];
_collectionView.delegate = self;
_collectionView.dataSource = self;
}
return _collectionView;
}
But this didn't worked and nothing is shown in my view (no error and empty view that's all) Also using debugger I've notified that the UICollectionView Method are never called
First of all, your collectionView method is not called because you are using _collectionView instead self.collectionView in your viewDidLoad method. You need to write self for every property to call their setter and getter methods.
Second, if you want to add custom layout without Storyboard of XIB, then you need to set it programmatically:
RFQuiltLayout* layout = [[RFQuiltLayout alloc] init];
layout.direction = UICollectionViewScrollDirectionVertical;
layout.blockPixels = CGSizeMake(100, 100);
self.collectionView.collectionViewLayout = layout;

Determine if current screen has visible navigation bar

I have a singlton object. Is there any simple way to determine if current screen contains a navigation bar within singlton methods?
The singleton is UIView subclass. It's designed for showing prorgess activity, e.g. network exchange. It looks like black rectangle dropping down from top and hiding when the work is done. Why singleton? It's easy to call it from any place of code
The followed snippet is showing the initialization of activity singleton and published here just for better understaning my idea.
-(void) showUpdatingView:(NSString *) msg {
[self initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 44)];
activity = [[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite] autorelease];
activity.frame = CGRectMake(5, 10, 22, 22);
labelView = [[[UILabel alloc] initWithFrame:CGRectMake(35, 10, [UIScreen mainScreen].bounds.size.width - 10, 22)] autorelease];
labelView.font = [UIFont boldSystemFontOfSize:12];
labelView.backgroundColor = [UIColor clearColor];
labelView.textColor = [UIColor whiteColor];
labelView.text = msg;
[self addSubview:activity];
[self addSubview:labelView];
self.backgroundColor = [UIColor blackColor];
self.alpha = 0.7;
}
The activity can be called by
[[ActivitySingleton getInstance] showUpdatingView:#"Getting data."];
it's not all.
The singleton is being created in AppDelegate object and the view is added to
inlineActivity = [[CHInlineActivityView alloc] initView];
[self.window.rootViewController.view addSubview:inlineActivity];
I know it may look crazy. But when I was designing it seemed to me reasonable
if you have all in one navigationController:
BOOL navHidden = self.window.rootViewController.navigationController.navigatonBarHidden;
if you don't it is a bit harder.. you could check the window's subviews and see if you can find a UINavigationBar
id navbar = [self.window firstSubviewOfKind:[UINavigationBar class] withTag:NSNotFound];
BOOL navHidden = navbar == nil;
#implementation NSView (findSubview)
- (NSArray *)findSubviewsOfKind:(Class)kind withTag:(NSInteger)tag inView:(NSView*)v {
NSMutableArray *array = [NSMutableArray array];
if(kind==nil || [v isKindOfClass:kind]) {
if(tag==NSNotFound || v.tag==tag) {
[array addObject:v];
}
}
for (id subview in v.subviews) {
NSArray *vChild = [self findSubviewsOfKind:kind withTag:tag inView:subview];
[array addObjectsFromArray:vChild];
}
return array;
}
#pragma mark -
- (NSView *)firstSubviewOfKind:(Class)kind withTag:(NSInteger)tag {
NSArray *subviews = [self findSubviewsOfKind:kind withTag:tag inView:self];
return subviews.count ? subviews[0] : nil;
}
#end

MasterViewController's TableViewController null in constructor?

I have the following MasterViewController:
- (id) init{
self = [super init];
if(self){
//self.title = #"Main Menu";
//self.clearsSelectionOnViewWillAppear = NO;
self.contentSizeForViewInPopover = CGSizeMake(320.0, 600.0);
UIImageView* imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"view-1_0000s_0000_Muskoka-Logo1"]];
self.navigationItem.titleView = imageView;
self.tableView.backgroundColor = [UIColor clearColor];
self.tableView.opaque = NO;
self.tableView.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"view-1_0002s_0003s_0001_Bottom-panel"]];
self.tableView.scrollEnabled = NO;
}
return self;
}
It crashes when I try to set the background colour of the tableviewcontroller because the tableview inside the master is null. The image there is not null of course.
My only guess is that [super init] is not working properly?
A view controller's views and subviews do not exist until they have been loaded, which will occur during loadView (where the view hierarchy is built either in code or from a xib) All of those customisations should be in viewDidLoad. I'm surprised it's crashing, though, messages to nil don't cause crashes.

UITableViewCell subviews issue

I load cell from xib. I added 3 vertical separators to the cell (tried to add from xib and from code). Everything is OK until I select the cell. When I select the sell, separators disappear.
Here are the images:
Not selected:
Selected
Code of adding separators:
- (id)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if (self) {
self.topSeparator = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, self.bounds.size.width, 1)] autorelease];
[self addSubview:topSeparator];
self.bottomSeparator = [[[UIView alloc] initWithFrame:CGRectMake(0, self.bounds.size.height - 1, self.bounds.size.width, 1)] autorelease];
[self addSubview:bottomSeparator];
// Initialization code
CGFloat xOffset = 314;
UIView *imageView1 = [[[UIView alloc] initWithFrame:CGRectMake(xOffset, 0, 1, self.bounds.size.height)] autorelease];
imageView1.tag = kTagBorder1;
imageView1.backgroundColor = [UIColor blackColor];
[self addSubview:imageView1];
xOffset = 487;
imageView1 = [[[UIView alloc] initWithFrame:CGRectMake(xOffset, 0, 1, self.bounds.size.height)] autorelease];
imageView1.tag = kTagBorder2;
imageView1.backgroundColor = [UIColor blackColor];
[self addSubview:imageView1];
xOffset = 573;
imageView1 = [[[UIView alloc] initWithFrame:CGRectMake(xOffset, 0, 1, self.bounds.size.height)] autorelease];
imageView1.tag = kTagBorder3;
imageView1.backgroundColor = [UIColor blackColor];
[self addSubview:imageView1];
}
return self;
}
I even tried this:
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
[self bringSubviewToFront:[self viewWithTag:kTagBorder1]];
[self bringSubviewToFront:[self viewWithTag:kTagBorder2]];
[self bringSubviewToFront:[self viewWithTag:kTagBorder3]];
[self setNeedsDisplay];
// Configure the view for the selected state
}
You should not add the lines as a child view of self (UITableViewCell). Instead, add it as a child of self.contentView - which is the view containing the components inside a UITableViewCell.
This article contains tips on how to customize a UITableView properly.
I fixed it. I used UIImageView with black image instead of uiview with black background. In this case view doesn't disappear.

Resources