[英]How to create UIScrollview inside UIScrollview programmatically
[英]How to create a UIScrollView Programmatically?
好的,所以這里的關鍵是我根本沒有使用 IB,因為我正在使用的視圖是以編程方式創建的。 UIView
覆蓋了屏幕的下半部分,上面有一堆按鈕。 但是,我想向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];
當我這樣做時,按鈕只是從我的屏幕上消失了。 那么我該如何正確地做到這一點呢? 感謝您的幫助!
代替:
UIScrollView *scroll = [UIScrollView alloc];
這樣做(將框架設置為您希望滾動視圖的大小):
UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:...];
這可能對您以編程方式創建 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];
嘗試這個,
{
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];
}
以編程方式創建一個 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];
以編程方式創建 UI 時,我lazy
使用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
}
}
}
簡單方法:如果需要,可以多次創建
- (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];
}
}
計算iOS地圖上兩點之間的距離[關閉]
導入 UIKit 導入 CoreLocation
類視圖控制器: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))
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.