好吧,所以这里的关键是我根本不使用IB,因为我正在使用的View是以编程方式创建的。 UIView
覆盖屏幕的下半部分,上面有一串按钮。但是,我想在UIView
添加更多按钮,而不是更大。为此,我想在视图中创建一个UIScrollView
,这样我就可以在屏幕上添加更多按钮,以便用户可以滚动到它们。我认为它是如何运作的。
self.manaView = [[[UIView alloc] initWithFrame:frame] autorelease];
self.manaView.backgroundColor = [UIColor purpleColor];
UIScrollView *scroll = [UIScrollView alloc];
scroll.contentSize = CGSizeMake(320, 400);
scroll.showsHorizontalScrollIndicator = YES;
[self.manaView addSubview:scroll];
代码的第一部分支持我的UIView
,效果很好,但我无法弄清楚如何以编程方式创建UIScrollView
并将其添加到视图中,然后添加按钮它。
UIButton *ret2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
ret2.tag = 102;
ret2.frame = CGRectMake(255, 5, 60, 50);
[ret2 setTitle:@"Return" forState:UIControlStateNormal];
[ret2 addTarget:self action:@selector(flipAction:) forControlEvents:UIControlEventTouchUpInside];
[scroll addSubview:ret2];
当我这样做时,按钮就会从屏幕上消失。那么我该怎么做呢?谢谢你的帮助!
答案 0 :(得分:26)
而不是:
UIScrollView *scroll = [UIScrollView alloc];
执行此操作(将帧设置为您想要滚动视图的大小):
UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:...];
答案 1 :(得分:24)
这可能对您以编程方式创建uiscrollview有所帮助 http://unconditionalloop.blogspot.com/2011/04/uiscrollview-programmatically.html
UIScrollView *scrollview = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
NSInteger viewcount= 4;
for(int i = 0; i< viewcount; i++) {
CGFloat y = i * self.view.frame.size.height;
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, y,self.view.frame.size.width, self .view.frame.size.height)];
view.backgroundColor = [UIColor greenColor];
[scrollview addSubview:view];
[view release];
}
scrollview.contentSize = CGSizeMake(self.view.frame.size.width, self.view.frame.size.height *viewcount);
[self.view addSubview:scrollview];
答案 2 :(得分:12)
试试这个,
{
scrollview=[[UIScrollView alloc]initWithFrame:CGRectMake(0,0,320,480)];
scrollview.showsVerticalScrollIndicator=YES;
scrollview.scrollEnabled=YES;
scrollview.userInteractionEnabled=YES;
[self.view addSubview:scrollview];
scrollview.contentSize = CGSizeMake(width,height);
[scrollview release];
}
答案 3 :(得分:3)
以Programitically方式创建UiScrollView
ScrView = [[UIScrollView alloc]initWithFrame:CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y, self.view.frame.size.width, self.view.frame.size.height)];
ScrView.showsVerticalScrollIndicator=YES;
[ScrView setBackgroundColor:[UIColor yellowColor]];
[ScrView setScrollEnabled:YES];
[ScrView setContentSize:CGSizeMake(0, 700)];
[ScrView setShowsHorizontalScrollIndicator:YES];
[ScrView setShowsVerticalScrollIndicator:YES];
[self.view addSubview:ScrView];
答案 4 :(得分:2)
在以编程方式创建UI时,我经常使用lazy
,如下所示:
class WorldViewController: UIViewController {
override func loadView() {
super.loadView()
view = scrollView
scrollView.addSubview(label0)
}
lazy var scrollView: UIScrollView = {
let instance = UIScrollView()
instance.backgroundColor = UIColor.blackColor()
return instance
}()
lazy var label0: UILabel = {
let instance = UILabel()
instance.text = "Ice caps are melting"
instance.textColor = UIColor.whiteColor()
instance.sizeToFit()
return instance
}()
var needLayoutContent = true
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
if needLayoutContent {
let bounds = scrollView.bounds
let contentSize = CGSizeMake(bounds.width * 1.5, bounds.height * 1.5)
label0.center = CGPointMake(contentSize.width / 2, contentSize.height / 2)
scrollView.contentSize = contentSize
scrollView.contentOffset = CGPointMake(bounds.width * 0.25, bounds.height * 0.25)
needLayoutContent = false
}
}
}
答案 5 :(得分:0)
简单方法: 如果您需要更多
,您可以多次创建- (void)viewDidLoad
{
[super viewDidLoad];
int x = 0;
int y = 10;
for(int i=0; i<5; i++)
{
UIScrollView *scrollview=[[UIScrollView alloc]initWithFrame:CGRectMake(x, y, 50, 50)];
scrollview.showsVerticalScrollIndicator=YES;
scrollview.scrollEnabled=YES;
scrollview.userInteractionEnabled=YES;
scrollview.backgroundColor = [UIColor greenColor];
[self.view addSubview:scrollview];
//scrollview.contentSize = CGSizeMake(50,50);
x=x+55;
//[self myscrollView];
}
}
答案 6 :(得分:0)
为iOS计算地图上2点之间的距离[关闭]
导入UIKit 导入CoreLocation
ViewController类:UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
var myLocation = CLLocation(latitude: 23.1929, longitude: 72.6156)
var myBuddysLocation = CLLocation(latitude: 23.0504, longitude: 72.4991)
var distance = myLocation.distance(from: myBuddysLocation) / 1000
print(String(format: "The distance to my buddy is %.01fkm", distance))
}
}