self.contentView.frame.width is be smaller than the real width for the cell in iPad, with iPhone its okay.
here the code of the custom cell
cellContentView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.contentView.frame.size.width, 100)];
cellContentView.backgroundColor = [UIColor whiteColor];
[self.contentView addSubview:cellContentView];
You can see what I mean with this photo
Click here
What should I do ?
SOLUTION
use: [UIScreen mainScreen].bounds.size.width
Thanks #santhu
Related
I added a bottom border to a uitableviewcell programmatically (iPhone - Create custom UITableViewCell top and bottom borders):
UIView *bottomLineView = [[UIView alloc] initWithFrame:CGRectMake(0, cell.bounds.size.height, self.view.bounds.size.width, 1)];
bottomLineView.backgroundColor = [UIColor grayColor];
[cell.contentView addSubview:bottomLineView];
It's not displaying on an ipad unless you highlight it. All the other cells below and above it have the background color set to clear.. It displays fine on the iPhone
Move the line one pixel up. Right now it's outside of the frame, getting overlayed by the next cell.
UIView *bottomLineView = [[UIView alloc] initWithFrame:CGRectMake(0,
cell.bounds.size.height - 1,
self.view.bounds.size.width,
1)];
I have an app that starts in Landscape mode and I have the following UIView that is created to go full screen.
- (void)loadSignupScreen {
CGFloat screenHeight = self.view.frame.size.height;
CGFloat screenWidth = self.view.frame.size.width;
self.signupContainer = [[UIView alloc] initWithFrame:self.view.bounds];
[self.signupContainer setAutoresizingMask:UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleBottomMargin|UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin|UIViewAutoresizingFlexibleTopMargin];
UIImageView *bgImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, screenWidth, screenHeight)];
[bgImageView setBackgroundColor:[UIColor darkGrayColor]];
[bgImageView setAlpha:0.8];
[self.signupContainer addSubview:bgImageView];
[self.view addSubview:self.signupContainer];
}
The problem is that when I rotate the screen to portrait it isn't full screen instead it only seems to be about 3/4 the length of the screen. How can I make it fill the screen on rotate? I don't have a problem using the autosizing in IB just when it is programmed.
Thanks
You are assigning the wrong autoresizingMask to the signupContainer. It should just be flexible height and width.
[self.signupContainer setAutoresizingMask:UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight];
Also the UIImageView needs an autoresizingMask.
UIImageView *bgImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, screenWidth, screenHeight)];
[bgImageView setAutoresizingMask:UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight];
I am facing a really strange issue:
I am instantiating multiple UIImageView inside a for loop with the method CGRectMake, the y origin I am giving seems to be totally wrong on the screen:
Here is my code:
- (void)makeTheView
{
self.view.backgroundColor = [UIColor whiteColor];
UIScrollView *header = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, 100)];
header.backgroundColor = [UIColor colorWithRed:254/255.0f green:255/255.0f blue:213/255.0f alpha:1.0f];
[self.view addSubview:header];
for (int i = 0; i < 10; i++) {
UIImageView *avatar = [[UIImageView alloc] initWithFrame:CGRectMake(5 + i * 75, 5, 70, 70)];
avatar.image = [UIImage imageNamed:#"bo_pic_baby5.jpg"];
[avatar.layer setCornerRadius:8.0];
avatar.layer.masksToBounds = YES;
NSLog(#"%f", avatar.frame.origin.y);
UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(0, 50, avatar.frame.size.width, 20)];
title.backgroundColor = [UIColor colorWithRed:148/255.0f green:148/255.0f blue:148/255.0f alpha:0.5f];
title.font = [UIFont fontWithName:#"Arial" size:15];
title.text = #"崔健";
title.textAlignment = NSTextAlignmentCenter;
title.textColor = [UIColor whiteColor];
[avatar addSubview:title];
[header addSubview:avatar];
}
}
According to this code avatar is within header at 5px from the top of header.
But the following is what I obtain visually:
note: when the white area begin, the header view stopped
This is not a real issue since I can reevaluate my frames like this :
CGRectMake(5 + i * 75, - 20, 70, 70)
but it looks really weird, and I am quite sure I am missing something totally trivial here...
I think this will be fixed by:
self.automaticallyAdjustsScrollViewInsets = NO;
Since iOS 7, view controllers automatically adjust scroll view insets so that the scroll view content is not hidden behind the navigation bar because it expects scroll views to start at the top of the screen.
However, the usual solution is to just set the scrollview frame.origin.y to 0.
Your Code is Absolutely Correct , As you are Adding the scrollview on (0,64) Position , So 64 will be count from Bottom of the Navigation Bar, If you want it on top (Just Below the Navigation bar), Change this declaration to as below :
UIScrollView *header = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 100)];
I want to set frame for background colour of selected tableviewCell. I am using the following code.
UIView * cellBackgroundView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
cellBackgroundView.backgroundColor = [UIColor clearColor];
UIView * selectedCellColor = [[UIView alloc] initWithFrame:CGRectMake(5, 0, 300, 44)];
selectedCellColor.backgroundColor = [UIColor colorWithRed:50.0/255.0 green:50.0/255.0 blue:50.0/255.0 alpha:1.0];
[cellBackgroundView addSubview:selectedCellColor];
cell.selectedBackgroundView = cellBackgroundView;
Which is working fine in iOS7. Even Tableview selection also not working in iOS6.
Following code working fine in Both iOS6 and iOS 7. But here i am not able to set frame for background colour of selected tableViewCell.
UIView *selectedCellColor = [[UIView alloc] init];
[selectedCellColor setFrame:CGRectMake(0, 0, 320, 44)];
selectedCellColor.backgroundColor = [UIColor colorWithRed:50.0/255.0
green:50.0/255.0
blue:50.0/255.0
alpha:1.0];
cell.selectedBackgroundView=selectedCellColor;
In cellForRowAtIndexPath:, call dequeueReusableCellWithIdentifier:forIndexPath: (not simply dequeueReusableCellWithIdentifier:. The reason is that now your cell is the correct size. Do not hard-code the cell size; get it as the bounds of the cell you are handed.
Now draw a UIImage in that size, constructing your frame-like drawing (the image itself is clear to begin with). Put it in a UIImageView and set the cell's selected background view to that image view.
This will work in iOS 6 and iOS 7.
Example (color and size arbitrary, just for illustration purposes):
UIGraphicsBeginImageContextWithOptions(cell.bounds.size, NO, 0);
// change this color as you like
[[UIColor redColor] setFill];
// fiddle with this size as you like
[[UIBezierPath bezierPathWithRect:CGRectMake(10,10,300,25)] fill];
UIImage* im = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageView* iv = [[UIImageView alloc] initWithImage:im];
cell.selectedBackgroundView = iv;
EDIT
Try this instead:
[cell.contentView setBackgroundColor:[UIColor redColor]];
UIView *selectedBG = [[UIView alloc]initWithFrame:cell.contentView.frame];
[selectedBG setBackgroundColor:[UIColor greenColor]];
[cell.selectedBackgroundView addSubview:selectedBG];
I am working on my app to make compatible to iphone 5. In the following code, donebutton works for iphone 4, but when it comes to iphone5 it is not touchable! I dont know why?
float screenSizeHeight=[UIScreen mainScreen].bounds.size.height;
if(screenSizeHeight==568)
{
UIImageView *my_image_view = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"Plain_Background2.png"]];
[my_image_view setFrame:CGRectMake(0,20,320,568)];
[self.view addSubview:my_image_view];
[self.view sendSubviewToBack:my_image_view];
imageScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 20, 320, 460)];
[doneButtonOutlet setFrame:CGRectMake(124,470,72,28)];
}
if(screenSizeHeight==480)
{
UIImageView *my_image_view = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"Plain_Background.png"]];
[my_image_view setFrame:CGRectMake(0,20,320,480)];
[self.view addSubview:my_image_view];
[self.view sendSubviewToBack:my_image_view];
imageScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 20, 320, 400)];
[doneButtonOutlet setFrame:CGRectMake(124,432,72,28)];
}
This is because in iPhone 5 scrollView height is more and it is above your button.
So change origin y of UIButton.
imageScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 20, 320, 460)];
// Here imageScrollView bottom is at 480 pixel and doneButtonOutlet origin is at 470 so either small height of scrollview or change button origin y..
[doneButtonOutlet setFrame:CGRectMake(124,470,72,28)];
Hope it helps you.