In this code example, i am trying to produce the following view hierarchy
Window -> background image -> scroll view -> text view
All i see however is
Window -> background image
What am i missing please?
-(void) viewWillAppear:(BOOL)animated {
UIScrollView *scrollWindow = [[UIScrollView alloc]
initWithFrame:CGRectMake(30, 30, 440, 212)];
UITextView *scrollableText = [[UITextView alloc] init];
[scrollableText setEditable:NO];
[scrollableText setText:#"Why, hello there"];
[scrollWindow addSubview:scrollableText];
UIImage *backgroundImage = [[UIImage alloc] initWithCGImage:
[UIImage imageNamed:#"about_bg.png"].CGImage];
UIImageView *backgroundView = [[UIImageView alloc]
initWithImage:backgroundImage];
[backgroundView addSubview:scrollWindow];
[[self view] addSubview:backgroundView];
}
Andrew is right about not making the scroll view a subview of the background UIImageView view. But the scroll view is invisible. Only its content (scrollableText) will show. And you haven't set scrollableText's frame, so it's effectively invisible too. Init like so:
[scrollableText setEditable:NO];
[scrollableText setText:#"Why, hello there"];
[scrollableText setFrame:CGRectMake(0, 0, 100, 100)];
And you should see it.
What you need is this hierarchy:
Window
background image view
scroll view
text view
Try adding two subviews to your window, not shove them all as subviews into each other.''-
- (void) viewWillAppear:(BOOL)animated {
UIScrollView *scrollWindow = [[UIScrollView alloc] initWithFrame:CGRectMake(30, 30, 440, 212)];
UITextView *scrollableText = [[UITextView alloc] init];
UIImageView *backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"about_bg.png"]];
scrollableText.editable = NO;
scrollableText.text = #"Why, hello there";
[scrollWindow addSubview:scrollableText];
[self.view addSubview:backgroundView];
[self.view addSubview:scrollWindow];
}
Related
I have important problem with UIScrollView.
I want to make one app like yahoo weather and using this source code : BTGlassScrollView
in this source code exist one view with name "foregroundView" that is info view. this info view appear when scrolling my ScrollView from bottom to top.
now I make and add my ScrollView by this code:
_viewScroller = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width + 2*blackSideBarWidth, self.view.frame.size.height)];
[_viewScroller setPagingEnabled:YES];
[_viewScroller setDelegate:self];
[_viewScroller setShowsHorizontalScrollIndicator:NO];
[self.view addSubview:_viewScroller];
_glassScrollView1 = [[BTGlassScrollView alloc] initWithFrame:self.view.frame BackgroundImage:[UIImage imageNamed:#"background3"] blurredImage:nil viewDistanceFromBottom:0 foregroundView:[self customView]];
[_viewScroller addSubview:_glassScrollView1];
in my code I write [self customView] that make me info view and this is customView function:
- (UIView *)customView {
//this method is for make UITableView
[self makeTableBuy:[[self.tableDic objectForKey:#"person"] count]];
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, _tableBuy.frame.size.width, _tableBuy.frame.size.height)];
view.layer.cornerRadius = 3;
view.backgroundColor = [UIColor colorWithWhite:0 alpha:.3];
[view addSubview:_tableBuy];
return view;
}
- (UITableView*)makeTableBuy:(int)numberOfRow {
_tableBuy = [[UITableView alloc] initWithFrame:CGRectMake(0,0,self.view.frame.size.width,numberOfRow*TABLE_HEIGHT) style:UITableViewStyleGrouped];
[_tableBuy setTag:1];
[_tableBuy setBackgroundColor:[UIColor clearColor]];
_tableBuy.showsVerticalScrollIndicator = NO;
_tableBuy.separatorStyle = UITableViewCellSeparatorStyleNone;
_tableBuy.rowHeight = TABLE_HEIGHT;
_tableBuy.scrollEnabled = NO;
_tableBuy.delegate = self;
_tableBuy.dataSource = self;
return _tableBuy;
}
now When I'm scrolling my ScrollView my info view (my tableview) moving to top and appear but my scrolling have so lagging.
please guide me how to fix this lag???
I'm trying to create a simple scrollView inside a tableview cell. Just like on app store, but with simpler scrolling.
The scrollview is showing up but its not scrolling at all. I've tried whole first page on google and many stackoverflow questions, can't seem to find what I'm doing wrong.
if (indexPath.row==0) {
// Clearing out cell background
[cell setBackgroundColor:[UIColor clearColor]];
[cell.contentView setBackgroundColor:[UIColor clearColor]];
// Imageviews for scrollview
UIImageView *imageview1 = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,320,123)];
UIImageView *imageview2 = [[UIImageView alloc] initWithFrame:CGRectMake(320,0,320,123)];
imageview1.image = [UIImage imageNamed:#"flash2.png"];
imageview2.image = [UIImage imageNamed:#"IMG_2458.JPG"];
// setting up scrollview
self.scrollViewHeader = [[UIScrollView alloc] initWithFrame:CGRectMake(0,0,320, 123)];
self.scrollViewHeader.scrollEnabled = YES;
self.scrollViewHeader.delegate = self;
[self.scrollViewHeader setUserInteractionEnabled:YES];
[self.scrollViewHeader setContentSize:CGSizeMake(320, 123)];
[self.scrollViewHeader addSubview:imageview1];
[self.scrollViewHeader addSubview:imageview2];
[cell.contentView addSubview:self.scrollViewHeader];
}
else{
// Normal table cells.
...
}
Thanks in advance for your answers.
In order for a scroll view to scroll, its content size must be greater than its bounds.
You have:
self.scrollViewHeader = [[UIScrollView alloc] initWithFrame:CGRectMake(0,0,320, 123)];
and then
[self.scrollViewHeader setContentSize:CGSizeMake(320, 123)];
In this case, they are both the same size, therefore it won't scroll. Do:
self.scrollViewHeader = [[UIScrollView alloc] initWithFrame:CGRectMake(0,0,160, 123)];
[self.scrollViewHeader setContentSize:CGSizeMake(320, 123)];
So I have a tableview inside a UIPopOverController and I attached a UISearchBar to the header of the tableview and I have the UISearchDisplayController displaying the correct results. However, I can't get the results to display within the UIPopoverController window. It's displaying behind the popover view into the DetailView covering half the screen (SplitViewController for iPad). How do I bring the results/tableview into the popover view? I tried adding the searchTableView to the popover view, but it just covers up the original tableview.
Here is the code of setting this up:
//Initialize Popover Controller
UIView *ccPopoverView = [[UIView alloc] init];
UIViewController* popoverTableContent = [[UIViewController alloc] init];
self.ccTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 44, 320, 400) style:UITableViewStylePlain];
self.ccTableView.dataSource = self;
self.ccTableView.delegate = self;
self.searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 320.0, 44.0)];
self.searchDisplayController = [[UISearchDisplayController alloc]initWithSearchBar:self.searchBar contentsController:self];
self.searchDisplayController.searchResultsDataSource = self;
self.searchDisplayController.searchResultsDelegate = self;
self.searchDisplayController.delegate = self;
searchBar.frame = CGRectMake(0, 0, 0, 38);
self.ccTableView.tableHeaderView = searchBar;
ccPopoverView.backgroundColor = [UIColor blackColor];
//Here is where I tried adding the searchResultsTableView as a subview.
//[ccPopoverView addSubview:self.searchDisplayController.searchResultsTableView];
[ccPopoverView addSubview:self.ccTableView];
popoverTableContent.view = ccPopoverView;
self.popoverTableController.delegate = self;
self.popoverTableController = [[UIPopoverController alloc] initWithContentViewController:popoverTableContent];
[self.popoverTableController setPopoverContentSize:CGSizeMake(320, 440) animated:NO];
Thanks!
I fixed the issue instead of adding self when I initialized the search bar, I needed to put in the popovercontroller as the initialization.
I have two views in my program. One is statusView, it's alpha is set to 0.8f. And below it is tableView. How do I disable statusView such way, that clicking on it would not click tableView. Settings userInteractionEnabled = false; didn't help.
Here is how i add views.
[self.view addSubview:_builded.view]; // obj with my tableView
info = [[Info alloc] init];
[self.view addSubview:info.view]; // statusView
If you add a subview like the following, the tableView underneath would not catch the user interaction.
UITableView *testTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];
testTableView.delegate = self;
testTableView.dataSource = self;
[self.view addSubview:testTableView];
UIView *statusView = [[UIView alloc] initWithFrame:CGRectMake(20, 20, 200, 200)];
statusView.backgroundColor = [UIColor redColor];
statusView.alpha = 0.8;
[self.view addSubview:statusView];
I created an application with a side menu like facebooks. I have a root view controller which adds a view over itself, then when the user presses the 'Menu' button it will slide the view over so you can see the menu.
My issue is that when the view first loads it looks like the UINavBar is about 20 pixels or so lower than it should be. (The below image looks a bit off centered because it is a modal horizontal flip)
It then automatically adjusts itself and moves up just under that status bar when the modal transition is complete.
Here is a screenshot of what happens when I press the "Menu" button. It slides the top view over to the right
As you can see in the images above I have the left arrow and right arrow frame background set to red so that I can see where the clickable / tappable region is. I have another image which I drew a blue line on that shows if I click the blue line or above then it will not trigger the right arrow. If I click in that same general area on the left side the menu button will trigger. I am able to reproduce this in both the simulator and on a mobile device (so I am not fat fingering the button).
It seems to me that since the navigation bar is loading lower than it is supposed to the iPhone thinks it is still in that position so when I click inside the UIButton frame it is still triggering the Menu and not triggering the arrow selectors. If I tap DIRECTLY in the middle of the arrows, it triggers the arrows selectors but just a tiny bit outside the image, it will not trigger the arrow selector.
Here is my RootViewController viewDidLoad
AppointmentViewController *appointmentViewController = [self.storyboard instantiateViewControllerWithIdentifier:#"AppointmentViewController"];
NSLog(#"B: %#",NSStringFromCGRect(appointmentViewController.view.frame));
self.navController = [[UINavigationController alloc] initWithRootViewController:appointmentViewController];
NSLog(#"B2: %#",NSStringFromCGRect(appointmentViewController.view.frame));
NSLog(#"Nav Bounds: %#",NSStringFromCGRect(self.navController.view.frame));
NSLog(#"Bounds: %#",NSStringFromCGRect(self.contentView.bounds));
self.navController.view.frame = self.contentView.bounds;
NSLog(#"Nav Bounds2: %#",NSStringFromCGRect(self.navController.view.frame));
[self addShadowToMenu];
[self.contentView addSubview:self.navController.view];
RootViewController Logs
2012-04-30 12:24:44.533 My-App[19929:fb03] B: {{0, 20}, {320, 460}}
2012-04-30 12:24:44.535 My-App[19929:fb03] B2: {{0, 20}, {320, 460}}
2012-04-30 12:24:44.535 My-App[19929:fb03] Nav Bounds: {{0, 0}, {320, 480}}
2012-04-30 12:24:44.535 My-App[19929:fb03] Bounds: {{0, 0}, {320, 460}}
2012-04-30 12:24:44.535 My-App[19929:fb03] Nav Bounds2: {{0, 0}, {320, 460}}
AppointmentViewController
self.title = [dataObj.preferences objectForKey:#"appts_name_plural"];
self.navigationItem.title = self.title;
dayView = [[APCalendarDayView alloc] initWithCalendarView:(APCalendarView *)self];
APListView = [self.storyboard instantiateViewControllerWithIdentifier:#"AppointmentListView"];
APListView.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
SideMenuView *myDelegate = [[SideMenuView alloc] init];
[self setSideMenuDelegate:myDelegate];
//set the delegate's currentViewController property so that we can add a subview to this View.
[sideMenuDelegate setCurrentViewController:self];
UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *today = [[UIBarButtonItem alloc] initWithTitle:#"Today" style:UIBarButtonItemStyleBordered target:nil action:#selector(todayPressed)];
NSArray *appointmentNavigationItems = [[NSArray alloc] initWithObjects:#"Day",#"List",#"Month", nil];
navcontrol = [[UISegmentedControl alloc] initWithItems:appointmentNavigationItems];
[navcontrol setSegmentedControlStyle:UISegmentedControlStyleBar];
[navcontrol addTarget:self action:#selector(appointmentNavigationClicked:) forControlEvents:UIControlEventValueChanged];
[navcontrol setSelectedSegmentIndex:0];
UIBarButtonItem *segmentItems = [[UIBarButtonItem alloc] initWithCustomView:navcontrol];
NSArray *items = [NSArray arrayWithObjects:today,flexSpace,segmentItems, flexSpace, nil];
[appointmentToolbar setItems:items animated:NO];
[appointmentToolbar setAutoresizesSubviews:YES];
[[self.view superview] addSubview:myDelegate.view];
[self setViewSizeOffset:(appointmentToolbar.bounds.size.height)];
CalendarDayView
- (void)addSubviewsToHeaderView:(UIView *)headerView
{
const CGFloat kChangeMonthButtonWidth = 46.0f;
const CGFloat kChangeMonthButtonHeight = 44.0f;
const CGFloat kMonthLabelWidth = 215.0f;
const CGFloat kHeaderVerticalAdjust = 7.f;
// Header background gradient
UIImageView *backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"Kal.bundle/kal_grid_background.png"]];
CGRect imageFrame = headerView.frame;
imageFrame.origin = CGPointZero;
backgroundView.frame = imageFrame;
[headerView addSubview:backgroundView];
[backgroundView setAutoresizesSubviews:YES];
[backgroundView setAutoresizingMask:UIViewAutoresizingFlexibleWidth];
// Create the previous month button on the left side of the view
CGRect previousMonthButtonFrame = CGRectMake(self.left, 0, kChangeMonthButtonWidth, kChangeMonthButtonHeight);
UIButton *previousMonthButton = [[UIButton alloc] initWithFrame:previousMonthButtonFrame];
[previousMonthButton setBackgroundColor:[UIColor redColor]];
[previousMonthButton setImage:[UIImage imageNamed:#"Kal.bundle/kal_left_arrow.png"] forState:UIControlStateNormal];
previousMonthButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
previousMonthButton.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
[previousMonthButton addTarget:self action:#selector(showPreviousDay) forControlEvents:UIControlEventTouchUpInside];
//[previousMonthButton setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleLeftMargin];
[headerView addSubview:previousMonthButton];
// Draw the selected month name centered and at the top of the view
CGRect monthLabelFrame = CGRectMake((self.frame.size.width/2.0f) - (kMonthLabelWidth/2.0f), kHeaderVerticalAdjust, kMonthLabelWidth, kMonthLabelHeight);
headerTitleLabel = [[UILabel alloc] initWithFrame:monthLabelFrame];
headerTitleLabel.backgroundColor = [UIColor clearColor];
headerTitleLabel.font = [UIFont boldSystemFontOfSize:22.f];
headerTitleLabel.textAlignment = UITextAlignmentCenter;
headerTitleLabel.textColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"Kal.bundle/kal_header_text_fill.png"]];
headerTitleLabel.shadowColor = [UIColor whiteColor];
headerTitleLabel.shadowOffset = CGSizeMake(0.f, 1.f);
//[self setHeaderTitleText:[logic selectedMonthNameAndYear]];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"E, MMM dd, yyyy"];
headerTitleLabel.text = [dateFormatter stringFromDate:dataObj.activeDate];
[headerView addSubview:headerTitleLabel];
// Create the next month button on the right side of the view
CGRect nextMonthButtonFrame = CGRectMake(self.frame.size.width - kChangeMonthButtonWidth, 0, kChangeMonthButtonWidth, kChangeMonthButtonHeight);
UIButton *nextMonthButton = [[UIButton alloc] initWithFrame:nextMonthButtonFrame];
nextMonthButton.frame = nextMonthButtonFrame;
[nextMonthButton setImage:[UIImage imageNamed:#"Kal.bundle/kal_right_arrow.png"] forState:UIControlStateNormal];
[nextMonthButton setBackgroundColor:[UIColor redColor]];
nextMonthButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
nextMonthButton.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
[nextMonthButton addTarget:self action:#selector(showFollowingDay) forControlEvents:UIControlEventTouchUpInside];
//[nextMonthButton setAutoresizingMask:UIViewAutoresizingFlexibleWidth];
[headerView addSubview:nextMonthButton];
}
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0.f, 0.f, self.frame.size.width, kHeaderHeight)];
headerView.backgroundColor = [UIColor grayColor];
[self addSubviewsToHeaderView:headerView];
[headerView setAutoresizesSubviews:YES];
[headerView setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight];
[self addSubview:headerView];
[self bringSubviewToFront:headerView];
If I move the headerView down farther it will allow me to select anywhere in the red area and have the arrow selector be triggered. However, I want it to match Apples calendar and be just under the navigation bar.
Anyone know why the navigation bar is blocking the top portion of the headerView from being tapped? To be clear the portion of the headerView that is being blocked is from the bottom of the blue line and up. The UIButton with the red background has an action tied to the frame so it should be triggering the event, however it appears the navbar is blocking half of the UIButton from receiving taps.
This behavior is due to the fact that UIWindow is not propagating touches that coincide with the nav bar frame. The only way to override this behavior is to subclass UIWindow and override the hitTest: method. This will allow you to detect touches in this area, and forward them to the appropriate responders.
Here is an example project showing a custom UIWindow subclass that will respond to touches any where on the screen.