UIButton not displayed - ios

I use the following code to add a UIButton and a UITableView to a UIViewController:
self.tableView = ({
UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
tableView.translatesAutoresizingMaskIntoConstraints = NO;
tableView.scrollEnabled = NO;
tableView.allowsSelection = NO;
tableView.dataSource = self;
tableView.delegate = self;
tableView;
});
self.refreshButton = ({
UIButton *button = [UIButton new];
button.translatesAutoresizingMaskIntoConstraints = NO;
[button setTitle:#"testtest" forState:UIControlStateNormal];
[button addTarget:self
action:#selector(clickRefreshButton)
forControlEvents:UIControlEventTouchUpInside];
button.titleLabel.numberOfLines = 1;
button.titleLabel.textAlignment = NSTextAlignmentRight;
button.titleLabel.adjustsFontSizeToFitWidth = NO;
button.alpha = 1.0;
button;
});
[self.view addSubview:self.tableView];
[self.view addSubview:self.refreshButton];
CGFloat tableViewUpPadding = 20.0f;
CGFloat navigationBarHeight = self.navigationController.navigationBar.frame.size.height;
CGFloat tabBarHeight = self.tabBarController.tabBar.frame.size.height;
CGFloat buttonOffset = 10.0f;
CGFloat buttonSideLength = 50.0f;
[self.tableView mas_makeConstraints:^(MASConstraintMaker *make){
make.top.equalTo(self.view.mas_top).offset(tableViewUpPadding);
make.bottom.equalTo(self.view.mas_bottom).offset(-tabBarHeight);
make.width.equalTo(self.view.mas_width);
make.left.equalTo(self.view.mas_left);
}];
[self.refreshButton mas_makeConstraints:^(MASConstraintMaker *make){
make.right.equalTo(self.view.mas_right).offset(-buttonOffset);
make.bottom.equalTo(self.view.mas_bottom).offset(-buttonOffset);
make.width.equalTo(#(buttonSideLength));
make.height.equalTo(#(buttonSideLength));
}];
Among which I use masonry to do auto-layout. However when I run the app the button doesn't appear, and not even in view debugging area.. Can somebody tell me why?
Or if I use strings and struts to create button, it still won't show :(
CGRect applicationFrame = [UIScreen mainScreen].bounds;
CGFloat screenWidth = CGRectGetWidth(applicationFrame);
CGFloat screenHeight = CGRectGetHeight(applicationFrame);
CGFloat buttonWidth = 40.0f;
CGFloat buttonHeight = 40.0f;
CGFloat spacing = 10.0f;
CGFloat buttonX = screenWidth - buttonWidth - spacing;
CGFloat buttonY = screenHeight - buttonHeight - spacing;
CGRect buttonFrame = CGRectMake(buttonX, buttonY, buttonWidth, buttonHeight);
[self.refreshButton setFrame:buttonFrame];
By the way, I found that if I want to change self.tableView's width using auto-layout, it's also impossible:
[self.tableView mas_makeConstraints:^(MASConstraintMaker *make){
make.top.equalTo(self.view.mas_top).offset(tableViewUpPadding);
make.bottom.equalTo(self.view.mas_bottom).offset(-tabBarHeight);
make.width.equalTo(self.view.as_width).offset(-offset);
make.left.equalTo(self.view.mas_left);
}];
It turns out that the tableview is still as wide as self.view

Related

auto-sizing UICollectionView in UITableView with pure code

I am trying to create one UITableView with header and UITableViewCell header is fixed size. and in each UITableViewCell I will create one UICollectionView. the UICollectionViewCell height is not fixed. so Here I want UICollectionViewCell is auto-sizing in UICollectionView and UICollectionView is auto-sizing in UITableViewCell.
But The height is not auto-sizing after implementation. Could you help me on this.
TableView init:
- (UITableView *)tableView
{
if (_tableView == nil) {
CGFloat top = 20;
_tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
_tableView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
_tableView.delegate = self;
_tableView.separatorStyle = 0;
_tableView.dataSource = self;
_tableView.estimatedRowHeight = 200;
_tableView.sectionHeaderHeight = 40;
_tableView.rowHeight = UITableViewAutomaticDimension;
_tableView.backgroundColor = [UIColor whiteColor];
}
return _tableView;
}
UICollectionView in UITableViewCell Init:
_collectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:flowLayout];
_collectionView.delegate = self;
_collectionView.dataSource = self;
_collectionView.bounces = NO;
_collectionView.showsHorizontalScrollIndicator = NO;
_collectionView.showsVerticalScrollIndicator = NO;
_collectionView.backgroundColor = [UIColor whiteColor];
_collectionView.translatesAutoresizingMaskIntoConstraints = NO;
[_collectionView registerClass:[KCollectionViewCell class] forCellWithReuseIdentifier:#"KCollectionViewCell"];
[self.contentView addSubview:_collectionView];
[_collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.bottom.equalTo(self.contentView);
make.left.equalTo(self.contentView).offset(20);
make.right.equalTo(self.contentView).offset(-20);
}];
UICollectionViewCell init:
-(void)initUI{
self.label = [[UILabel alloc] init];
self.label.text = #"aaaaaaaaaaaaaaaa";
self.label.textColor = XUIColorMain;
self.label.textAlignment = NSTextAlignmentCenter;
self.label.layer.borderColor =[UIColor orangeColor].CGColor;
self.label.clipsToBounds = YES;
[self.label sizeToFit];
}
- (UICollectionViewLayoutAttributes *)preferredLayoutAttributesFittingAttributes:(UICollectionViewLayoutAttributes *)layoutAttributes {
UICollectionViewLayoutAttributes *attributes = [super preferredLayoutAttributesFittingAttributes:layoutAttributes];
CGRect frame = [self.label.text boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, 30) options:(NSStringDrawingUsesLineFragmentOrigin) attributes:[NSDictionary dictionaryWithObjectsAndKeys:self.label.font,NSFontAttributeName, nil] context:nil];
frame.size.width += 10;
frame.size.height = 30;
attributes.frame = frame;
return attributes;
}

How to bring an image to front in ScrollView

I'm showing images in the ScrollView but I want to know please how can I bring each image to the front in the middle while I'm scrolling. Also it is on the back on each other as in the picture below.
My code:
-(void)scrollView2{
_scrlView.backgroundColor = [UIColor blackColor];
_scrlView.pagingEnabled = NO;
_scrlView.bounces = false;
_scrlView.delegate = self;
_scrlView.showsHorizontalScrollIndicator = YES;
_scrlView.layer.cornerRadius = 2;
_scrlView.contentSize = CGSizeMake(_scrlView.frame.size.width, _scrlView.frame.size.height);
_scrlView.contentSize = CGSizeMake(_scrlView.frame.size.width * 2, _scrlView.frame.size.height);
CGRect screenRect = [[UIScreen mainScreen] bounds];
CGFloat screenWidth = screenRect.size.width;
CGFloat screenHeight = screenRect.size.height;
int segment = screenWidth / 5;
int screenX2 = screenHeight/2 - segment/2;
__block float x = (screenWidth-segment)/2;
NSArray *arrImages=[NSArray arrayWithObjects:#"Smiley-1.png",#"Smiley-2.png",#"Smiley-3.png",#"Smiley-4.png",#"Smiley-5.png", nil];
for(int i=0;i<arrImages.count;i++)
{
UIImageView *imgVw=[[UIImageView alloc]init];
[imgVw setFrame:CGRectMake(x, screenX2, segment, segment)];
[imgVw setImage:[UIImage imageNamed:[NSString stringWithFormat:#"%#",[arrImages objectAtIndex:i]]]];
[imgVw setBackgroundColor:[UIColor redColor]];
[imgVw setContentMode:UIViewContentModeScaleToFill];
[_scrlView addSubview:imgVw];
x=x+segment;
}
[self.view addSubview:_scrlView];
}
float oldY;
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
int scrollX = scrollView.contentOffset.x;
int position = scrollX/segment;
NSLog(#"X: %f , position: %d", scrollView.contentOffset.x , position);
[_scrlView setContentOffset: CGPointMake(_scrlView.contentOffset.x, oldY)];
}
You can use bring sub view to front. For instance
[scrollView bringSubviewToFront:_img1];
I understood what you meant, there is an interesting project. It should help you.
You can also try yourself using a UICollectionView.
- (void)viewDidLoad
{
[super viewDidLoad];
NSArray *arrImages=[NSArray arrayWithObjects:#"smiley1",#"smiley2",#"smiley3",#"smiley4",#"smiley5",#"smiley6", #"smiley7",#"smiley8", nil];
screenRect = [[UIScreen mainScreen] bounds];
screenWidth = screenRect.size.width;
screenHeight = screenRect.size.height;
UIScrollView *scrlView=[[UIScrollView alloc]init];
[scrlView setFrame:CGRectMake(0, 0, 320, 568)];
scrlView.backgroundColor = [UIColor blackColor];
scrlView.pagingEnabled = YES;
scrlView.bounces = false;
scrlView.delegate = self;
scrlView.showsHorizontalScrollIndicator = YES;
scrlView.layer.cornerRadius = 2;
segmentWidth = screenWidth / 5;
long ScrollContentSize = ((arrImages.count -1)*segmentWidth)+screenWidth;
scrlView.contentSize = CGSizeMake(ScrollContentSize, scrlView.frame.size.height);
float screenX2 = screenHeight/2 - segmentWidth/2;
__block float screenX = (screenWidth-segmentWidth)/2;
for(int i=0;i<arrImages.count;i++)
{
UIImageView *imgVw=[[UIImageView alloc]init];
[imgVw setFrame:CGRectMake(screenX, screenX2, segmentWidth+30, segmentWidth+30)];
[imgVw setImage:[UIImage imageNamed:[NSString stringWithFormat:#"%#",[arrImages objectAtIndex:i]]]];
[imgVw setTag:5000+i];
[imgVw setContentMode:UIViewContentModeScaleAspectFit];
screenX = screenX+segmentWidth;
[scrlView addSubview:imgVw];
if (i%2==0)
{
[imgVw setBackgroundColor:[UIColor clearColor]];
[scrlView sendSubviewToBack:imgVw ];
}
else
{
[imgVw setBackgroundColor:[UIColor clearColor]];
[scrlView bringSubviewToFront:imgVw];
}
}
[self.view addSubview:scrlView];
}
Try this

Quick movement change of image on swipe scrollview

In the below code when clicked on page control it moving to another Image, with an quick movement.
Need same effect of quick movement swipe on scroll View.
-(void)viewDidLoad{
pageControlBeingUsed = NO;
pageControl = [[UIPageControl alloc]init];
scrollView = [[UIScrollView alloc] init];
scrollView.delegate = self;
scrollView.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height/1.6);
scrollView.bounces = true;
scrollView.showsHorizontalScrollIndicator = false;
scrollView.showsVerticalScrollIndicator = false;
NSArray *colors = [NSArray arrayWithObjects:[UIColor colorWithPatternImage:[UIImage imageNamed:#"image1"]], [UIColor colorWithPatternImage:[UIImage imageNamed:#"image2"]],[UIColor colorWithPatternImage:[UIImage imageNamed:#"image3"]],nil];
for (int i = 0; i < colors.count; i++){
CGRect frame;
frame.origin.x = self.scrollView.frame.size.width * i;
frame.origin.y = 0;
frame.size = self.scrollView.frame.size;
UIView *subview = [[UIView alloc] initWithFrame:frame];
subview.backgroundColor = [colors objectAtIndex:i];
[self.scrollView addSubview:subview];
}
scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width * colors.count, self.scrollView.frame.size.height);
pageControl.frame = CGRectMake(0,self.scrollView.frame.size.height+10, self.view.frame.size.width,30);
[pageControl addTarget:self action:#selector(changePage:) forControlEvents:UIControlEventValueChanged];
pageControl.currentPage = 0;
pageControl.numberOfPages = colors.count;
[self.view addSubview:pageControl];
[self.view bringSubviewToFront:pageControl];
[self.view addSubview:scrollView];
}
- (void)scrollViewDidScroll:(id)sender {
if (!pageControlBeingUsed) {
CGFloat pageWidth = self.scrollView.frame.size.width;
int page = floor((self.scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
self.pageControl.currentPage = page;
}
}
- (void)changePage:(id)sender{
CGRect frame;
frame.origin.x = self.scrollView.frame.size.width * self.pageControl.currentPage;
frame.origin.y = 0;
frame.size = self.scrollView.frame.size;
[self.scrollView scrollRectToVisible:frame animated:YES];
pageControlBeingUsed = YES;
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
pageControlBeingUsed = NO;
}
From above code when i click on pageControl its trigger the method changePage and move to next page, same effect need when user swipe from left to right right to left on scrollview.
#All
Just Add scrollView.pagingEnabled = YES; Issue resolved.

Why isn't my UIPageControl updating when the ScrollView scrolls?

I'm trying to make a table whose cells have UIPageControls with ScrollViews in them. The ScrollViews work and the PageControls are there, but the PageControl does not update when scrolling happens except for on the bottom cell. Here is my code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Cell"];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.contentView.backgroundColor = [UIColor blueColor];
UIColor *color = [colorsArray objectAtIndex:indexPath.row];
scrollView = [[UIScrollView alloc] init];
[scrollView setDelegate:self];
CGRect screen = [[UIScreen mainScreen] bounds];
CGFloat width = CGRectGetWidth(screen);
CGFloat height = tableView.rowHeight;
scrollView.frame = CGRectMake(0, 0, width, height/1.25);
scrollView.contentSize=CGSizeMake(width*3,height/1.25);
scrollView.pagingEnabled = YES;
[scrollView setBackgroundColor:color];
[scrollView setShowsHorizontalScrollIndicator:NO];
scrollView.clipsToBounds = YES;
NSUInteger i;
int xCoord=width/25;
int yCoord=width/25;
int buttonWidth=width/8;
int buttonHeight=width/8;
int buffer = width;
for (i = 1; i <= 4; i++)
{
UIButton *aButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
aButton.backgroundColor = [UIColor blackColor];
aButton.frame = CGRectMake(xCoord, yCoord,buttonWidth,buttonHeight );
[aButton addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
aButton.tag = i;
UIImage *buttonImage = [picsArray objectAtIndex:indexPath.row];
[aButton setBackgroundImage:buttonImage forState:UIControlStateNormal];
[scrollView addSubview:aButton];
xCoord += buttonHeight + buffer;
}
[cell addSubview:scrollView];
pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, height/1.25, width, height/4)];
pageControl.numberOfPages = 3;
pageControl.currentPage = 0;
pageControl.userInteractionEnabled =YES;
[pageControl addTarget:self action:#selector(pageAction:) forControlEvents:UIControlEventValueChanged];
[cell addSubview:pageControl];
return cell;
}
- (void)scrollViewDidScroll:(UIScrollView *)sender {
// Update the page when more than 50% of the previous/next page is visible
CGFloat pageWidth = scrollView.frame.size.width;
int page = floor((scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
pageControl.currentPage = page;
}
- (void)pageAction:(UIPageControl *)sender {
// update the scroll view to the appropriate page
CGRect frame;
frame.origin.x = scrollView.frame.size.width * pageControl.currentPage;
frame.origin.y = 0;
frame.size = scrollView.frame.size;
[ scrollView scrollRectToVisible:frame animated:YES];
}
That is because you have reference only to scroll view and page control generated for the last cell:
// this references are redefined every time new cell is generated
scrollView = [[UIScrollView alloc] init];
pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, height/1.25, width, height/4)];
You could try to use sender in your UIScrollView delegate method:
- (void)scrollViewDidScroll:(UIScrollView *)sender {
// Update the page when more than 50% of the previous/next page is visible
CGFloat pageWidth = sender.frame.size.width;
int page = floor((sender.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
// !!! but here is another problem. You should find reference to appropriate pageControl
pageControl.currentPage = page;
}
But there will be one more problem: you should get reference to the appropriate UIPageControl object. You could use tags, array or anything else for that purpose.

Using UIScrollView with paging enabled

I have implemented paging using UIScrollView and adding UIVIew and UIControls in this UIVIew on each pages. Surprisingly none of the UIControl responding when it is on UIScrollView with paging enabled! Following is my code. Could anyone please tell me how to get UIControls working on UIScrollView with paging enabled?
- (void)viewDidLoad
{
[super viewDidLoad];
AppDelegate *delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
int totalPages = [[delegate sections] count] + 2; // 2 for startOfDayQuestions and endOfDayQuestions
int X = 45, Y = 20, H = 40, W = 680;
// start of the day questions
CGRect startFrame = scrollView.frame;
startFrame.origin.x = 0;
startFrame.origin.y = 0;
UIView *startPage = [[UIView alloc] initWithFrame:startFrame];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(X, Y, W, H)];
label.backgroundColor = [UIColor clearColor];
label.textColor = [UIColor blackColor];
label.text = #"Text";
label.font = [UIFont fontWithName:paragraph_font_name size:paragraph2_font_size];
[startPage addSubview:label];
Y += H;
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(X, Y, W, H)];
textField.borderStyle = UITextBorderStyleRoundedRect;
textField.font = [UIFont systemFontOfSize:15];
textField.placeholder = #"enter text";
textField.autocorrectionType = UITextAutocorrectionTypeNo;
textField.keyboardType = UIKeyboardTypeDefault;
textField.returnKeyType = UIReturnKeyDone;
textField.clearButtonMode = UITextFieldViewModeWhileEditing;
textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
textField.delegate = self;
textField.userInteractionEnabled = YES;
[startPage addSubview:textField]; --> This UITextField doesn't respond when added to startPage and in turns scrollView but works fine if I add it to self.view!
// a page is the width of the scroll view
scrollView.pagingEnabled = YES;
scrollView.userInteractionEnabled = YES;
scrollView.contentSize = CGSizeMake(scrollView.frame.size.width * totalPages, scrollView.frame.size.height);
scrollView.showsHorizontalScrollIndicator = NO;
scrollView.showsVerticalScrollIndicator = NO;
scrollView.scrollsToTop = NO;
scrollView.delegate = self;
pageControl.numberOfPages = totalPages;
pageControl.currentPage = 0;
}
- (void)scrollViewDidScroll:(UIScrollView *)sender
{
if (pageControlUsed)
return;
CGFloat pageWidth = scrollView.frame.size.width;
int page = floor((scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
pageControl.currentPage = page;
}
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
pageControlUsed = NO;
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
pageControlUsed = NO;
}
- (IBAction)changePage:(id)sender
{
int page = pageControl.currentPage;
CGRect frame = scrollView.frame;
frame.origin.x = frame.size.width * page;
frame.origin.y = 0;
[scrollView scrollRectToVisible:frame animated:YES];
pageControlUsed = YES;
}
Adding subScrollView with no pagingEnabled to each page and controls into subScrollView works! If you add controls directly to scrollview with paging enabled, it seems gesture recogniser's first responder is always scrollview so your controls never gets the event and behave like a disabled!

Resources