Creating UIScrollView In Interface Builder Using Dynamic Content - ios

I am a newcomer to the realm of iOS development.
I am trying to get the UIScrollView control to work and ran across the following question:
steps for creating UIScrollView with Interface Builder
I followed the steps outlined in the answer for this question and everything works as I want. However, this appears to create a statically-sized view that is scrolled. What I am really after is a dynamically sized view that may or may not be scrolled. For example, instead of the view with buttons, I put a single label for which I set the text and number of lines in the viewDidLoad method. The view that contains the label is set to a static size so the scroll viewer does not attempt to scroll and the content of the label spills off of the page.
What am I missing?

you need to set te content size of the scroll
CGPoint controlsCenter = ViewBottom.center;
if(Pointsvalue > 5)
{
controlsCenter.y += txtFldFrame1.frame.origin.y+20;
ViewBottom.center = controlsCenter;
[ScrollLag addSubview:ViewBottom];
}
[ScrollLag setContentSize:(CGSizeMake(0, controlsCenter.y+100))];
make sure your label height changes on entering text.......

The URL in my comment to the original question provides a solution to the issue I raised.

I have a dynamically sized UIScrollView in a project I am working on right now. This is what I did:
self.myScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 250, 748)];
[self.myScrollView setBackgroundColor:[UIColor blackColor]];
int heightCounter = 10;
UIButton *newButton = [UIButton buttonWithType:UIButtonTypeCustom];
newButton.frame = CGRectMake(10, heightCounter, 200, 40);
[newButton setBackgroundColor:[UIColor clearColor]];
[newButton addTarget:self action:#selector(someMethod:) forControlEvents:UIControlEventTouchUpInside];
[newButton setTitle:#"ButtonTitle" forState:UIControlStateNormal];
[newButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[self.myScrollView addSubview:newButton];
heightCounter += 50;
// add additional items to the myScrollView and remember to increase the heightCounter
self.myScrollView.contentSize = CGSizeMake(250, heightCounter);
self.myScrollView.scrollEnabled = YES;
[self.view addSubview:self.myScrollView];

Related

How to show buttons as grid on scroll view and tapped on button show more labels on beneath

I have scroll view with buttons and i want to show more labels on beneath of selected button tapped and remaining buttons are moved to next position
I am objective c
Please any one can help me
My Code is here
UIScrollView *scrollView = [[UIScrollView alloc]initWithFrame:self.view.frame];
[self.view addSubview:scrollView];
for (int i = 0; i<83; i++){
imageButton = [UIButton buttonWithType:UIButtonTypeCustom];
[imageButton setTranslatesAutoresizingMaskIntoConstraints:YES];
// [imageButton setBackgroundColor:[UIColor blackColor]];
[imageButton setBackgroundImage:[UIImage imageNamed:#"books"] forState: UIControlStateNormal];
[imageButton setTitle:[NSString stringWithFormat:#" %d",i] forState:UIControlStateNormal];
[imageButton setFrame:CGRectMake(xPossion, yPossion, 70, 60)];
imageButton.highlighted=YES;
[imageButton addTarget:self
action:#selector(butnSelected:)
forControlEvents:UIControlEventTouchUpInside];
[scrollView addSubview:imageButton];
xPossion += imageButton.frame.size.width+35;
temp++;
if (temp==3) {
yPossion = imageButton.frame.origin.y+imageButton.frame.size.height+20;
temp = 0;
xPossion = 20;
yPossion += imageButton.frame.size.width-15;
[scrollView setContentSize:CGSizeMake(scrollView.frame.size.width ,yPossion-50)];
}
}
My target is set to screen as like below enter image description here
and when I click on one image i want to display sub categories like belowenter image description here
You can use UISTackView for your purpose. Add circular / category buttons as well as all other buttons in one stackview. Hide / show the sub categories button on click of circular / category button. One of the main advantages of the stack view is that it will automatically create Auto Layout constraints for each subview that's added to it. You also have finite control over how those subviews are sized and positioned. There are options to configure the sizing of the views, where they should be arranged, as well as how much padding should be between the subviews.
I would also give a try and let you know.

View with subviews in a uinavigationbar and uitoolbar

I really need your help with this one (first post on SO -- be gentle):
I have two dynamic UIButtons which I would like to have centered in a UIView, which in turn should be centered in a UINavigationbar and UIToolbar. I can't - despite a lot of Googling - seem to figure out a proper way to do this.
This is what I've done so far:
In viewDidLoad, I add the two buttons as subviews and set the view as the UINavigationbar's titleView
self.myClass.viewForTitleAndButton = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 120, 32)];
[self.myClass.viewForTitleAndButton setBackgroundColor:[UIColor blackColor]];
[self.myClass.viewForTitleAndButton addSubview:self.myClass.myButton];
[self.myClass.viewForTitleAndButton addSubview:self.myClass.myOtherButton];
self.navigationItem.titleView = self.myClass.viewForTitleAndButton;
In a method being triggered when I press certain buttons, I set the title (and bounds) of one of the buttons depending on what's clicked:
CGSize titleSize = [title sizeWithAttributes:#{NSFontAttributeName : [UIFont italicSystemFontOfSize:17.0]}];
CGSize screenSize = [UIScreen mainScreen].bounds.size;
CGFloat newX = (screenSize.width - titleSize.width) / 2;
CGRect buttonFrame = self.myClass.myButton.frame;
//Removing the line below doesn't do any difference at the moment
self.myClass.myButton.bounds = CGRectMake(newX, buttonFrame.origin.y, titleSize.width+8, buttonFrame.size.height);
[self.myClass.myButton setTitle:title forState:UIControlStateNormal];
NSLog(#"Title: %#", title);
//title is a NSString that changes depending on what is clicked. I am 100% sure it changes as I can see it in the log every time the method is triggered.
The problem is that the title of myButton is not changed. It worked before with the very same button when it was placed in a different spot and not as a subview.
Q1: What am I missing to make the title of the button change?
Q2: Is adding the buttons as subViews to a view that is then placed in the navigationbar and toolbar respectively a sound way to accomplish what I want?
This is really bugging me, any pointers in the right direction is much appreciated.
I don't think you can add a button which is already an outlet of a view controller. Thinking in another way, a button(view) can only has one superview.
Create button dynamically for the title view.
- (void)viewDidLoad
{
[super viewDidLoad];
UIView *iv = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 160, 44)];
iv.backgroundColor = [UIColor greenColor];
titleButton = [UIButton buttonWithType:UIButtonTypeCustom];
titleButton.frame = CGRectMake(0, 0, 120, 44);
[titleButton setTitle:#"test" forState:UIControlStateNormal];
[titleButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[iv addSubview:titleButton];
self.navigationItem.titleView = iv;
}
//some event to change the button title
- (void)click:(UIButton *)button
{
[titleButton setTitle:#"I changed" forState:UIControlStateNormal];
}

Get rid of padding for UITableViewCell custom accessoryView

I have a table view cell with a custom accessoryView, but it seems that the way it's laid out by default creates a 15-point margin on the right side. I want to get rid of it.
I can achieve the desired effect by overriding layoutSubviews, but that breaks edit mode animation (the accessory no longer slides in/out).
Any better way to do this?
Add subview instead of accessoryView
UIButton *deleteBtn = [UIButton buttonWithType:UIButtonTypeCustom];
deleteBtn.frame = CGRectMake(cell.contentView.frame.size.width-55, 2, 50, 50);
[deleteBtn setBackgroundImage:[UIImage imageNamed:#"trash.png"]
forState:UIControlStateNormal];
deleteBtn.backgroundColor = [UIColor clearColor];
//deleteBtn.alpha = 0.5;
deleteBtn.tag = indexPath.row;
[deleteBtn addTarget:self action:#selector(numberDeleteConfirm:)
forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:deleteBtn];

UIScrollView does not scroll

I got a problem about UIScrollView. I am making a custom view which inherits UIView. The view has a UIScrollView on which there are lots of buttons which should scroll left and right. The UIScrollView and buttons can show normally. But I cannot scroll the buttons. Could someone give me some suggestions? Thanks a lot!
MZMPhotoCalenderSwitcher.h
#import <UIKit/UIKit.h>
#interface MZMPhotoCalenderSwitcher : UIView <UIScrollViewDelegate>
#property (strong, nonatomic) UIScrollView *topSwitcher;
#end
MZMPhotoCalenderSwitcher.m
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
self.topSwitcher = [[UIScrollView alloc] initWithFrame:CGRectMake(0, LABEL_HEIGHT + VIEW_Y, self.view.bounds.size.width, TOP_SWITCHER_HEIGHT)];
self.topSwitcher.backgroundColor = [UIColor greenColor];
self.topSwitcher.pagingEnabled = YES;
self.topSwitcher.showsHorizontalScrollIndicator = NO;
self.topSwitcher.showsVerticalScrollIndicator = NO;
[self add:3 ButtonsOnView:self.topSwitcher withButtonWidth:44.8f andHeight:20.0f];
}
- (void)add:(int)num ButtonsOnView:(UIScrollView *)view withButtonWidth:(CGFloat)width andHeight:(CGFloat)height
{
CGFloat totalTopSwitcherWidth = num * width;
[view setContentSize:CGSizeMake(totalTopSwitcherWidth, view.bounds.size.height)];
CGFloat xOffset = 0.0f;
for (int i=1; i<=num; i++)
{
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setFrame:CGRectMake(xOffset, 0, width, height)];
xOffset += width;
[button setTitle:[NSString stringWithFormat:#"%d", i] forState:UIControlStateNormal];
button.titleLabel.font = [UIFont systemFontOfSize:10];
[button setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
[button setTitleColor:[UIColor blackColor] forState:UIControlStateSelected];
[button setTag:i];
[button addTarget:self action:#selector(buttonEvent) forControlEvents:UIControlEventTouchUpInside];
if (i % 2 == 0)
[button setBackgroundColor:[UIColor yellowColor]];
else
[button setBackgroundColor:[UIColor redColor]];
[view addSubview:button];
}
}
Below following line
[view addSubview:button];
add
view.contentSize = CGSizeMake(view.contentSize.width, button.frame.origin.y + button.frame.size.height);
This will set content size of scroll view to the bottom of your button.
It should be issue of content size, also check that scrollEnabled property of UIScrollView object is set to TRUE,
Check solution of UIScrollView won't scroll!
Just disable "Auto Layout" in your project and it will start working!
I'm using Xcode 6/iOS 8+ now with auto-layout turned on, and I had the same problem in the first place, removing auto-layout doesn't work at all, so in order to make scroll view scrollable vertically, I made sure the following:
The scroll view content size height MUST be bigger than the screen
height, this almost goes without saying...
The top/bottom/left/right constraint of the scroll view HAS TO BE
pinned, I did this using storyboard so no code showing up here
If you want to make the scroll view scrollable horizontally, then make sure its content size width is bigger than the screen width, the other rule applies the same.
This worked for me, hope it can help someone.
Combining all of this advise into one simple answer:
// In viewDidLoad
// set the size of the scrollview (in this case I made it the size of its container
self.scrollView.frame = self.view.frame;
// now set the size of the content to be scrolled within the scrollview
// the content to be displayed must be bigger than the scrollView
// in this case I added 100 to make the content size, so that it is taller than the height of the scrollView
// now it scrolls
self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width, self.scrollView.frame.size.height + 100);
Your problem is that you haven't set your contentSize, therefore, the contentSize is the same than the frame view.
You need to do this in your viewDidLoad
- (void)viewDidLoad {
........
self.scrollView.contentSize = CGSizeMake(/*yourWidth*/, /*yourHeight/*);
........
}
Try do adjust your contentSize to the last position of your object plus its size plus a padding.
You have to set scrollview frame size and content size.
Where are you add ScrollView on SuperVIew? And where are you recalculate Content size property of scrollView?
[view setContentSize:CGSizeMake(<summYourViews.width>,<summYourViews.height>)];
try this . . . .
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
self.topSwitcher = [[UIScrollView alloc] initWithFrame:CGRectMake(0, LABEL_HEIGHT + VIEW_Y, self.view.bounds.size.width, TOP_SWITCHER_HEIGHT)];
self.topSwitcher.backgroundColor = [UIColor greenColor];
self.topSwitcher.pagingEnabled = YES;
self.topSwitcher.showsHorizontalScrollIndicator = NO;
self.topSwitcher.showsVerticalScrollIndicator = NO;
[topSwitcher setContentSize:CGPointMake(self.view.bounds.size.width,TOP_SWITCHER_HEIGHT+400)];
[self add:3 ButtonsOnView:self.topSwitcher withButtonWidth:44.8f andHeight:20.0f];
}
It's amazing.
I just move the codes which was in viewDidLoad to initWithFrame:, then it works. I do not know why it is?

Set the button frame fit to the view

I want to display a UIButton in the header section of a UITableView. This is the code I have tried
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0,0, 285, 44)];
UIButton *headerViewButton = [UIButton buttonWithType:UIButtonTypeCustom];
headerViewButton.frame = CGRectMake(headerView.bounds.size.width * 7/5, headerView.bounds.size.height * 1/4, 320.0, 30.0);
headerViewButton.backgroundColor = [UIColor grayColor];
[headerViewButton setTitle:#"Import main list from other field >" forState:UIControlStateNormal];
[headerViewButton addTarget:self
action:#selector(buttonPressed:)
forControlEvents:UIControlEventTouchDown];
[headerView addSubview:headerViewButton];
The problem is I am having a hard time adjusting the button frame, so that it fits fine in the landscape mode as well as portrait mode. I am not very comfortable with using Interface Builder. How can I adjust the button frame correctly so that it fits the view.
I have tried using setAutoresizingMask
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0,0, 320, 44)];
UIButton *headerViewButton = [UIButton buttonWithType:UIButtonTypeCustom];
headerViewButton.frame = CGRectMake(headerView.bounds.size.width * 7/5, headerView.bounds.size.height * 1/4, 290, 30.0);
headerViewButton.backgroundColor = [UIColor grayColor];
[headerViewButton setTitle:#"Import main list from other field >" forState:UIControlStateNormal];
[headerViewButton addTarget:self
action:#selector(buttonPressed:)
forControlEvents:UIControlEventTouchDown];
[headerViewButton setAutoresizingMask:UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin|UIViewAutoresizingFlexibleWidth];
[headerView addSubview:headerViewButton];
As the app loads for the first time, the button is missing, but as I change the orientation, the button appears. Every time I try, its the same pattern. The first time the app loads, the button would be missing, and it appears on both orientations after that.
You need to set the button's autoresizingMask to appropriate values. The values depend on how you want the button's size and/or position to adjust as its parent view's size changes.
In the code you posted, it is odd that you are making the button wider than its parent view.
Try setting
[headerViewButton setAutoresizingMask:UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth];
This way it will be resized as the superview changes. (Works the same as setting autoresizing options in IB)

Resources