Adding sub view to UITableViewHeader getting wrong width? - ios

First I build a view add to tableview as tableHeaderView
UIView *mainHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 300)];
[self.mainViewTableView setTableHeaderView:mainHeaderView];
I could adjust the Height of mainHeaderView but I cant adjust Y position of this headView.
And I trying to add a sub view:
self.searchForShop = [[UISearchBar alloc]initWithFrame:CGRectMake(0, 0, self.mainViewTableView.tableHeaderView.frame.size.width, 44)];
[self.mainViewTableView addSubview:self.searchForShop];
Run app and found the subview's width is longer than tableHeadView.
Do view debug and found the subview is 16 width more than tableHeadView(I run in iphone5s).
How to fix it?

u can try like this, there might be a problem with setting the width of the tableview, just set the width of search bar to width of tableview
//create a tableview
tableView=[[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
[self.view addSubview:self.tableView];
self.tableView.delegate=self;
self.tableView.dataSource=self;
UISearchBar *searchBar=[[UISearchBar alloc]init];
/* option 1 ->[searchBar sizeToFit]; */
/* option 2 ->*/ searchBar.frame = CGRectMake(0, 0, self.tableView.bounds.size.width, 44); //set the width how much table view has
searchBar.delegate=self;
[self.tableView setTableHeaderView:searchBar];

Related

Q:When I add UIScrollView to a UIView, but the scrollView can not scroll

I add a UIScrollView to a customView, and I set the right contentSize, but the scrollView can not scroll.
This is my code:
_scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 44, self.bounds.size.width, self.bounds.size.height - 44)];
// _scrollView.translatesAutoresizingMaskIntoConstraints = NO;
// _scrollView.userInteractionEnabled = NO;
_scrollView.contentSize = CGSizeMake(3 * self.bounds.size.width, self.bounds.size.height - 44 + 500);
_scrollView.contentOffset = CGPointMake((_orginSelectedIndex)*(self.bounds.size.width), 44);
_scrollView.pagingEnabled = YES;
_scrollView.scrollEnabled = YES;
[self addSubview:_scrollView];
/* add tableviews */
for (int i = 0 ; i < _titlesArr.count; i ++) {
UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(i * self.bounds.size.width, 44, self.bounds.size.width, self.bounds.size.height - 44) style:UITableViewStylePlain];
tableView.tag = 200 + i;
tableView.backgroundColor = RandColor;
[self addSubview:tableView];
}
And I also Log my scroll's contentSize.
NSLog(#"%#-s", NSStringFromCGSize(lml_pager.scrollView.contentSize));
There show :
2016-06-21 17:39:07.272 LMLViewPager[7381:312567] {1125, 1059}-s
Somebody know what the issue is? I did not use autolayout in my code.
And there is my picture token in the debug:
Obviously,it only add one tableView, my _titlesArr count is 3.
picture
tableview should be added to scrollview instead of scrollview's superview.
Make sure that you have user interaction turned on as well.
You are adding UITableView on top of the UIScrollView which actually put the the scrollView below the tableView. If you want the tableView to be a part of the scrollView. Try to add tableView as subview of the scrollView
Your view hierarchy is like below :
YourCustomView->ScrollView->TableView
Now you are trying to scroll, scrollview but you can see that Tableview is on top of hierarchy. So it won't scroll horizontally.

Adding a UILabel as a subview on top of a UITextView

I am trying to make a fake placeholder for a UITextView with a UILabel. Let's say there's a UITextView inside ViewController and this is what I do:
CGFloat frameWidth = self.postInputField.frame.size.width;
textViewPlaceholder = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, frameWidth, 200)];
textViewPlaceholder.text = POST_PLACEHOLDER;
textViewPlaceholder.textColor = [UIColor lightGrayColor];
[self.view addSubview:textViewPlaceholder];
[super viewWillAppear:animated];
}
And there's an unnecessary margin on top of the UILabel as shown below:
I could set the y value to be negative something but I want to make sure why this is happening. One thing that I do when the view controller is loaded is self.automaticallyAdjustsScrollViewInsets = NO; to get rid of the margin inside the UITextView, but it shouldn't matter.
The exact x y values that work are (4, -16) for the UILabel.
I think the problem is the height of UILabel:
textViewPlaceholder = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, frameWidth, 200)];
The height is too large. Try to reduce it. And using background color to see more.

UIScrollView is not scrolling even with setting contentSize

My UIScrollView isn't scrolling. (I'm trying to get it to scroll horizontally) I'm setting the contentSize to be (960, 300). I have set the framesize of the UIScrollView to be width:320 height:300 in the storyboard.
I have a container view inside the scroll view that has a width of 960 points.
At 320 points, I have a subview that has a background color of brown. When I scroll, I can see the brown subview, but it bounces back when I let go of the drag.
This is my viewDidLoad method:
- (void)viewDidLoad
[super viewDidLoad];
self.scrollView.scrollEnabled = YES;
[self.scrollView setFrame:CGRectMake(0,0,320,300)];
self.scrollView.contentSize = CGSizeMake(960, 300);
UIView *subview1 = [[UIView alloc] initWithFrame:CGRectMake(320, 0, 320, 300)];
[subview1 setBackgroundColor:[UIColor brownColor]];
UIView *containerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 960, 300)];
[containerView addSubview:subview1];
[self.scrollView addSubview:containerView];
}
Here is a sample code for create a scroll view programmatically:
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIScrollView* scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 300)];
scrollView.backgroundColor = [UIColor redColor];
scrollView.scrollEnabled = YES;
scrollView.contentSize = CGSizeMake(960, 300);
[self.view addSubview:scrollView];
UIView *subview1 = [[UIView alloc] initWithFrame:CGRectMake(320, 0, 320, 300)];
[subview1 setBackgroundColor:[UIColor brownColor]];
UIView *containerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 960, 300)];
containerView.backgroundColor = [UIColor greenColor];
[containerView addSubview:subview1];
[scrollView addSubview:containerView];
So, there is no issue in your code, but something wrong with your storyboard configure.
I have a few thoughts about what you are trying to accomplish. I don't know how old is your code, but today we have many better ways to work with scrollview and they don't include setting up the intrinsic size or fame.
Well, despite my doubt about the objective of the code, I tried to run it here, using storyboard, a single view, a default configured scrollView and the experiment went well, your code actually works, I think maybe you have some problem with your scrollView. I know this will sound weird but did you checked if the scrollView has the property "Scrolling Enabled" checked?
If you can give me more information about this issue, I can help you.

ScrollView inside another scrollView

I have two UIScrollViews (red and yellow). Red is a subView of Yellow and grey is my device screen. I'm able make the scrolling works in both directions (vertical & horizontal), but there is one problem... Vertical scroll only works if the horizontal is SCROLLED FIRST?
Both of these views are NOT the firstResponder. I tried setting [yellow becomeFirstResponder] but it returned NO.
How do I make either UIScrollView scrollable upon user's touch?
you can do this by following code
UIScrollView *scrollYellow = [[UIScrollView alloc] initWithFrame:CGRectMake(40, 40, 250, 480)];
[scrollYellow setBackgroundColor:[UIColor yellowColor]];
[scrollYellow setContentSize:CGSizeMake(250, 800)];
[self.view addSubview:scrollYellow];
UIScrollView *scrollRed = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 80, 250, 200)];
[scrollRed setBackgroundColor:[UIColor redColor]];
[scrollRed setContentSize:CGSizeMake(500, 200)];
[scrollYellow addSubview:scrollRed];
hear in scrollYellow make width of frame & ContentSize same
and in scrollRed make height of frame & ContentSize same

UIScrollView won't stay in place after user finishes scrolling

Within my UIView, I have a UIScrollView which fills the first view, so than when the content is bigger than the iPhone screen size, the user can scroll the page down. It works well, but when the user finishes the scroll movement - i.e. removes his fingers, the page snaps back into it's original position. Obviously that is not what I want, how can it be avoided?
Here is the relevant code in the UIView class which declares and uses the UIScrollView class.
#implementation TestView
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
// Initialization code.
}
CGRect scrollViewFrame = CGRectMake(0, 0, 320, 460);
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:scrollViewFrame];
scrollView.canCancelContentTouches=NO;
[self addSubview:scrollView];
CGSize scrollViewContentSize = CGSizeMake(320, 500);
[scrollView setContentSize:scrollViewContentSize];
CGRect rectForBigRedSquare = CGRectMake(50, 50, 200, 200);
UILabel *redSquare = [[UILabel alloc] initWithFrame:rectForBigRedSquare];
[redSquare setBackgroundColor:[UIColor redColor]];
[scrollView addSubview:redSquare];
return self;
}
An additional question is this: how is it possible to make it such that the user can only scroll down, that is to see content at the bottom which was out of view, but not to scroll up so that there is space before the start of the content. In
Basically you just have to set contentSize of your scrollview according to the contents.
CGSize scrollViewSize = CGSizeMake(newWidth, newHeight);
[self.myScrollView setContentSize:scrollViewSize];
Okay, the easiest way to get this scrollview working as you desire is to ensure that content size of the scrollview is identical to the frame size of the content you wish to scroll.
You can achieve this by having a content view into which you add all the views you wish to be visible and then add that content view to the scrollview while ensuring that the content size of the scrollview is set to the content view's frame size.
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];
UIView* contentView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1280, 460)];
UIView* redView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];
[redView setBackgroundColor:[UIColor redColor]];
[contentView addSubview:redView];
[redView release];
UIView* blueView = [[UIView alloc] initWithFrame:CGRectMake(960, 0, 320, 460)];
[redView setBackgroundColor:[UIColor blueColor]];
[contentView addSubview:blueView];
[blueView release];
CGSize contentViewSize = contentView.frame.size;
[scrollView addSubview:contentView];
[scrollView setContentSize:contentViewSize];
[contentView release];
[self addSubview:scrollView];
The app I was working on had similar symptoms. The user could scroll down but on release the view would snap back to the initial position. The page was set up as follow:
[VIEW]
[SAFE AREA]
[SCROLL VIEW]
[CONTENT VIEW]
I strongly suspect that a combination of Auto-Layout and manual constraints caused by several adjustment iterations was causing the issue. To resolve this all constraints where removed from the View.
The Scroll View was assigned the following constraints:
Scroll View.leading = Safe Area.leading
Scroll View.top = Safe Area.top
Scroll View.trailing= Safe Area.trailing
Scroll View.bottom = Safe Area.bottom
The Content View was then assign the following constraints
ContentView.Leading = Scroll View.Leading
ContentView.top = Scroll View.top
ContentView.centerX = ScrollView.centerX
The Content View was also given the following self constraint
height = 1000

Resources